/** * Test variable and variations. * * @since 2.7.0 */ function test_variables_and_variations() { $product = new WC_Product_Variable(); $product->set_name('Variable Product'); $attribute = new WC_Product_Attribute(); $attribute->set_id(0); $attribute->set_name('pa_color'); $attribute->set_options(explode(WC_DELIMITER, 'green | red')); $attribute->set_visible(false); $attribute->set_variation(true); $product->set_attributes(array($attribute)); $product->save(); $this->assertEquals('Variable Product', $product->get_name()); $variation = new WC_Product_Variation(); $variation->set_name('Variation #1 of Dummy Variable CRUD Product'); $variation->set_parent_id($product->get_id()); $variation->set_regular_price(10); $variation->set_sku('CRUD DUMMY SKU VARIABLE GREEN'); $variation->set_manage_stock('no'); $variation->set_downloadable('no'); $variation->set_virtual('no'); $variation->set_stock_status('instock'); $variation->set_attributes(array('pa_color' => 'green')); $variation->save(); $this->assertEquals('Variation #1 of Dummy Variable CRUD Product', $variation->get_name()); $this->assertEquals('CRUD DUMMY SKU VARIABLE GREEN', $variation->get_sku()); $this->assertEquals(10, $variation->get_price()); $product = new WC_Product_Variable($product->get_id()); $children = $product->get_children(); $this->assertEquals($variation->get_id(), $children[0]); $expected_attributes = array('pa_color' => array('green')); $this->assertEquals($expected_attributes, $product->get_variation_attributes()); $variation_2 = new WC_Product_Variation(); $variation_2->set_name('Variation #2 of Dummy Variable CRUD Product'); $variation_2->set_parent_id($product->get_id()); $variation_2->set_regular_price(10); $variation_2->set_sku('CRUD DUMMY SKU VARIABLE RED'); $variation_2->set_manage_stock('no'); $variation_2->set_downloadable('no'); $variation_2->set_virtual('no'); $variation_2->set_stock_status('instock'); $variation_2->set_attributes(array('pa_color' => 'red')); $variation_2->save(); $this->assertEquals('Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name()); $this->assertEquals('CRUD DUMMY SKU VARIABLE RED', $variation_2->get_sku()); $this->assertEquals(10, $variation_2->get_price()); $product = new WC_Product_Variable($product->get_id()); $children = $product->get_children(); $this->assertEquals($variation_2->get_id(), $children[1]); $this->assertEquals(2, count($children)); $expected_attributes = array('pa_color' => array('green', 'red')); $this->assertEquals($expected_attributes, $product->get_variation_attributes()); $variation_2->set_name('UPDATED - Variation #2 of Dummy Variable CRUD Product'); $variation_2->set_regular_price(15); $variation_2->set_sale_price(9.99); $variation_2->set_date_on_sale_to('32532537600'); $variation_2->save(); $product = new WC_Product_Variable($product->get_id()); $expected_prices['price'][$children[0]] = 10.0; $expected_prices['price'][$children[1]] = 9.99; $expected_prices['regular_price'][$children[0]] = 10.0; $expected_prices['regular_price'][$children[1]] = 15.0; $expected_prices['sale_price'][$children[0]] = 10.0; $expected_prices['sale_price'][$children[1]] = 9.99; $this->assertEquals($expected_prices, $product->get_variation_prices()); $this->assertEquals('UPDATED - Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name()); $product->set_name('Renamed Variable Product'); $product->save(); $this->assertEquals('Renamed Variable Product', $product->get_name()); $product->delete(); }
/** * Convert a WC Product or Variation into a Square ItemVariation * See: https://docs.connect.squareup.com/api/connect/v1/#datatype-itemvariation * * @param WC_Product|WC_Product_Variation $variation * @param bool $include_inventory * @return array Formatted as a Square ItemVariation */ public static function format_wc_variation_for_square_api($variation, $include_inventory = false) { $formatted = array('name' => null, 'pricing_type' => null, 'price_money' => null, 'sku' => null, 'track_inventory' => null, 'inventory_alert_type' => null, 'inventory_alert_threshold' => null, 'user_data' => null); if ($variation instanceof WC_Product) { $formatted['name'] = __('Regular', 'woocommerce-square'); $formatted['price_money'] = array('currency_code' => apply_filters('woocommerce_square_currency', get_woocommerce_currency()), 'amount' => $variation->get_display_price() * 100); $formatted['sku'] = $variation->get_sku(); if ($include_inventory && $variation->managing_stock()) { $formatted['track_inventory'] = true; } } if ($variation instanceof WC_Product_Variation) { $formatted['name'] = implode(', ', $variation->get_variation_attributes()); } return array_filter($formatted); }
function wpp_export($export) { error_reporting(0); ob_clean(); $export = addslashes(substr(strip_tags($_GET['wpp_export']), 0, 5)); @set_time_limit(864000); if (ini_get('max_execution_time') != 864000) { @ini_set('max_execution_time', 864000); } $post_id = url_to_postid($_SERVER['REQUEST_URI']); $post = get_post($post_id); if ($post->post_status != 'publish') { return false; } if (post_password_required($post->ID)) { return false; } $product = get_product($post->ID); $out = ''; if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) { $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'shop_single'); $wpp_featured_image = $thumbnail[0] ? $thumbnail[0] : $thumbnail[0]; } else { $wpp_featured_image = ''; } if ($thumbnail[1]) { $wpp_featured_image = '<img width="' . $thumbnail[1] . '" height="' . $thumbnail[2] . '" src="' . $thumbnail[0] . '"/>'; } $wpp_price_html = strip_tags($product->get_price_html()); $wpp_title = $post->post_title; $wpp_sku = $product->get_sku(); $wpp_permalink = $product->get_permalink(); $wpp_stock = $product->get_stock_quantity(); $wpp_rating = strip_tags($product->get_rating_html()); $wpp_categories = $product->get_categories(', '); $wpp_tags = $product->get_tags(', '); $wpp_summary = wpp_text_filter($post->post_excerpt); $wpp_description = wpp_text_filter($post->post_content); $attribute_html = "\r\n<ul>"; if ($product->enable_dimensions_display()) { $attribute_html .= $product->get_dimensions() ? '<li>' . get_option('wpp_ph_dimensions') . ': ' . $product->get_dimensions() . "\r\n</li>" : ''; } $attribute_html .= $product->get_weight() ? '<li>' . get_option('wpp_ph_weight') . ': ' . $product->get_weight() . ' ' . get_option('woocommerce_weight_unit') . "\r\n</li>" : ''; if ($product->has_attributes()) { $wpp_attributes = $product->get_attributes(); if (!empty($wpp_attributes)) { foreach ($wpp_attributes as $attribute) { if (!$attribute['is_visible']) { continue; } if ($attribute['is_taxonomy']) { $product_terms = wp_get_post_terms($post->ID, $attribute['name']); $p_terms = array(); //$attribute_name = ucwords(str_replace('attribute_','',str_replace('pa_','',$attribute['name']))); $attribute_name = wc_attribute_label($attribute['name']); foreach ($product_terms as $p_term) { $p_terms[] = $p_term->name; } $attribute_html .= '<li>' . $attribute_name . ': ' . implode(', ', $p_terms) . "\r\n</li>"; } else { $attribute_html .= '<li>' . $attribute['name'] . ': ' . $attribute['value'] . "\r\n</li>"; } } } } $attribute_html .= '</ul>'; $attribute_html = str_replace(array(' |', '|'), ',', $attribute_html); $wpp_has_variants = false; $wpp_variants = "\r\n<ul>"; $args = array('post_type' => 'product_variation', 'post_status' => 'publish', 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'asc', 'post_parent' => $post->ID); $variations = get_posts($args); if (!empty($variations)) { $wpp_has_variants = true; foreach ($variations as $variation) { $vars = new WC_Product_Variation($variation->ID); $wpp_variants .= '<li>' . str_replace($vars->get_sku(), '', html_entity_decode(strip_tags(wpp_text_filter(str_replace('–', ' - ', $vars->get_formatted_name()))), ENT_QUOTES, "UTF-8")) . "\r\n</li>"; unset($vars); } $wpp_variants .= '</ul>'; $wpp_variants = str_replace(array('<li> - ', ', - '), array('<li>', ' - '), $wpp_variants); } //Post Specific Custom Tabs if ($product_meta = get_post_meta($post->ID)) { foreach ($product_meta as $meta_key => $meta_value) { if (strpos($meta_key, '_wpt_field_') !== FALSE) { $custom_tab_slug = str_replace('_wpt_field_', '', $meta_key); $custom_tab = get_posts(array('name' => $custom_tab_slug, 'posts_per_page' => 1, 'post_type' => 'woo_product_tab', 'post_status' => 'publish')); if (trim($meta_value[0])) { $custom_tab_title_html[] = $custom_tab[0]->post_title; $custom_tab_content_html[] = wpp_text_filter($meta_value[0]); } } continue; } } //General Custom Tabs $general_custom_tab = get_posts(array('posts_per_page' => -1, 'post_type' => 'woo_product_tab', 'post_status' => 'publish')); foreach ($general_custom_tab as $tab) { if (trim($tab->post_content)) { $cfkey = '_wpt_field_' . $tab->post_name; if (get_post_meta($post->ID, $cfkey, true)) { //it's already taken } else { $custom_tab_title_html[] = $tab->post_title; $custom_tab_content_html[] = wpp_text_filter($tab->post_content); } } } $wpp_gallery = $product->get_gallery_attachment_ids(); foreach ($wpp_gallery as $wpp_img) { $wpp_gallery_images .= ' ' . wp_get_attachment_image($wpp_img, 'shop_single') . ' '; } if ($export == 'doc') { $doc_title = stripslashes($post->post_title); $wpp_description = stripslashes($wpp_description); $wpp_price = get_option('wpp_ph_price') . ': ' . html_entity_decode(str_replace('–', ' - ', $product->get_price_html()), ENT_QUOTES, "UTF-8"); if (get_option('wpp_save_doc_img_max_width')) { $wpp_max_image_width = get_option('wpp_save_doc_img_max_width'); } else { $wpp_max_image_width = 500; } if (preg_match_all('|width=[\'\\"]*(\\d+)[\'\\"]*[^\\>\\<]+height=[\'\\"]*(\\d+)[\'\\"]*|i', $wpp_description, $doc_img_sizes, PREG_SET_ORDER)) { foreach ($doc_img_sizes as $doc_img) { $doc_img_w = $doc_img[1]; $doc_img_h = $doc_img[2]; if ((int) $wpp_max_image_width < (int) $doc_img_w) { $doc_img_h = $doc_img_h * ($wpp_max_image_width / $doc_img_w); $doc_img_w = $wpp_max_image_width; } $wpp_description = preg_replace('|width=[\'\\"]*' . $doc_img[1] . '[\'\\"]*[^\\>\\<]+height=[\'\\"]*' . $doc_img[2] . '[\'\\"]*|i', 'width="' . $doc_img_w . '" height="' . $doc_img_h . '"', $wpp_description); } } $wpp_description = preg_replace('|\\[caption[^\\]]+\\]([^\\]]*)\\[\\/caption\\]|is', '$1', $wpp_description); $wpp_description = preg_replace(array('|<embed[^>]+>[^>]*<\\/embed>|i', '|<object[^>]+>|i', '|<\\/object>|i', '|<param[^>]+>|i', '|<script[^>]+>[^>]*<\\/script>|i'), '', $wpp_description); preg_match_all('|<img[^>]+(class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+[^\'\\">]+[\'\\"]+)[^>]*>|i', $wpp_description, $body_array, PREG_SET_ORDER); if (!empty($body_array)) { for ($i = 0; $i < count($body_array); $i++) { $replace = preg_replace('|(.+)class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+align([^\\s\\t\\r\\n\'\\">]+)[^\'\\">]*[\'\\"]+(.+)|i', '</span><![if !vml]> $1 align = \'$2\' $3 <![endif]><span style=\'font-size:10.5pt;color:#666666;\'>', $body_array[$i][0]); $wpp_description = str_replace($body_array[$i][0], $replace, $wpp_description); } } $wpp_doc_urls = wpp_img_url($wpp_description); if (!empty($wpp_doc_urls)) { foreach ($wpp_doc_urls as $k => $v) { $wpp_description = str_replace($k, $v, $wpp_description); } } $doc_post_date = $post->post_date; $doc_post_date_gmt = $post->post_date_gmt; $doc_modified_date = $post->post_modified; $doc_modified_date_gmt = $post->post_modified_gmt; if (!get_option('wpp_doc_t_image')) { $wpp_description = preg_replace('|<img[^><]+>|i', '', $wpp_description); } $doc_t_title = $doc_title && get_option('wpp_doc_t_title') ? '<h2 style="color:#000000;text-align:' . get_option('wpp_save_text_align') . '">' . $doc_title . '</h2><br>' : ''; $doc_t_date = get_option('wpp_doc_t_date') ? get_option('wpp_ph_prod_date') . ': ' . $doc_post_date . '<br>' : ''; $doc_t_md_date = get_option('wpp_doc_t_md') ? get_option('wpp_ph_prod_mod_date') . ': ' . $doc_modified_date : ''; require WPP_FOLDER . 'template/header/doc_head.php'; if (get_option('wpp_save_doc_template')) { require WPP_FOLDER . 'template/save_as_word_document.php'; } else { require WPP_FOLDER . 'template/save_as_word_document_oo.php'; } $doc = trim($doc); $length = strlen($doc); $file = trim(str_replace(' ', '-', trim($doc_title)), '-'); $file = $file . '.doc'; header('Content-Type: application/msword; charset=utf-8'); if (headers_sent()) { echo 'Some data has already been output, can\'t send MS Doc file'; } header('Content-Length: ' . $length); header('Content-Disposition: attachment; filename=' . $file); header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public'); ini_set('zlib.output_compression', '0'); echo $doc; } elseif ($export == 'pdf') { global $wpp_html_link; $html_title = stripslashes($post->post_title); $wpp_html_link = stripslashes($post->guid); //Price Style $wpp_price = $product->get_price_html(); $wpp_price = strpos($wpp_price, '') === FALSE ? $wpp_price : str_replace('руб', 'руб', strip_tags($wpp_price)); if ($product->is_on_sale()) { $wpp_price = str_replace(array('<del>', '</del>'), array('^[font][' . get_option('wpp_ph_real_price') . ': ', '][200,200,200][14]^<br> ^[font][' . get_option('wpp_ph_curr_sale_price') . ':'), str_replace('–', ' - ', $wpp_price)) . '][15, 117, 84][14]^'; } else { $wpp_price = '^[font][' . get_option('wpp_ph_price') . ': ' . strip_tags($wpp_price) . '][15, 117, 84][14]^'; } //Limiting image sizes preg_match_all('|<img[^>]+(class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+[^\'\\">]+[\'\\"]+)[^>]*>|i', $html_body, $body_array, PREG_SET_ORDER); if (!empty($body_array)) { for ($i = 0; $i < count($body_array); $i++) { $replace = preg_replace('|(.+)class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+align([^\\s\\t\\r\\n\'\\">]+)[^\'\\">]*[\'\\"]+(.+)|i', '$1 align = "$2" style="padding:5px" $3', $body_array[$i][0]); $html_body = str_replace($body_array[$i][0], $replace, $html_body); } } $html_body = preg_replace('|<noscript>.+?</noscript>|is', '', $html_body); $html_body = preg_replace("|\r|", "<br>", $html_body); $wpp_pdf_urls = wpp_img_url($html_body); if (!empty($wpp_pdf_urls)) { foreach ($wpp_pdf_urls as $k => $v) { if (strpos($k, '../') === FALSE) { $html_body = str_replace($k, $v, $html_body); } else { continue; } } } require WPP_FOLDER . 'template/header/pdf_head.php'; require WPP_FOLDER . 'template/save_as_pdf.php'; require WPP_FOLDER . 'pdf.php'; } elseif ($export == 'print') { $print_title = stripslashes($post->post_title); $wpp_description = stripslashes($wpp_description); $wpp_price = get_option('wpp_ph_price') . ': ' . html_entity_decode(str_replace('–', ' - ', $product->get_price_html()), ENT_QUOTES, "UTF-8"); if (get_option('wpp_save_print_img_max_width')) { $wpp_max_image_width = get_option('wpp_save_print_img_max_width'); } else { $wpp_max_image_width = 500; } if (preg_match_all('|width=[\'\\"]*(\\d+)[\'\\"]*[^\\>\\<]+height=[\'\\"]*(\\d+)[\'\\"]*|i', $wpp_description, $print_img_sizes, PREG_SET_ORDER)) { foreach ($print_img_sizes as $print_img) { $print_img_w = $print_img[1]; $print_img_h = $print_img[2]; if ((int) $wpp_max_image_width < (int) $print_img_w) { $print_img_h = $print_img_h * ($wpp_max_image_width / $print_img_w); $print_img_w = $wpp_max_image_width; } $wpp_description = preg_replace('|width=[\'\\"]*' . $print_img[1] . '[\'\\"]*[^\\>\\<]+height=[\'\\"]*' . $print_img[2] . '[\'\\"]*|i', 'width="' . $print_img_w . '" height="' . $print_img_h . '"', $wpp_description); } } $wpp_description = preg_replace('|\\[caption[^\\]]+\\]([^\\]]*)\\[\\/caption\\]|is', '$1', $wpp_description); $wpp_description = preg_replace(array('|<embed[^>]+>[^>]*<\\/embed>|i', '|<object[^>]+>|i', '|<\\/object>|i', '|<param[^>]+>|i', '|<script[^>]+>[^>]*<\\/script>|i'), '', $wpp_description); if (get_option('wpp_pt_links')) { preg_match_all("|<a href[\\s\n\t\r]*=[\\s\n\t\r]*[\\'\"]+([^>\\'\"]+)[\\'\"]+[^><]*>([^><]+)</a>|i", $wpp_description, $links_array, PREG_SET_ORDER); if (!empty($links_array)) { for ($i = 0; $i < count($links_array); $i++) { $print_links .= '<li>' . wpp_phrase_spliter($links_array[$i][1], 50, ' ', false) . '</li>'; $wpp_description = str_replace($links_array[$i][0], '<u>' . $links_array[$i][0] . '</u> <sup>' . ($i + 1) . '</sup>', $wpp_description); } } } preg_match_all('|<img[^>]+(class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+[^\'\\">]+[\'\\"]+)[^>]*>|i', $wpp_description, $body_array, PREG_SET_ORDER); if (!empty($body_array)) { for ($i = 0; $i < count($body_array); $i++) { $replace = preg_replace('|(.+)class[\\s\\n\\t\\r]*=[\\s\\n\\t\\r]*[\'\\"]+align([^\\s\\t\\r\\n\'\\">]+)[^\'\\">]*[\'\\"]+(.+)|i', '$1 align = "$2" style="padding:5px" $3', $body_array[$i][0]); $wpp_description = str_replace($body_array[$i][0], $replace, $wpp_description); } } $print_excerpt = stripslashes($post->post_excerpt); $print_post_date = $post->post_date; $print_post_date_gmt = $post->post_date_gmt; $print_modified_date = $post->post_modified; $print_modified_date_gmt = $post->post_modified_gmt; if (!get_option('wpp_pt_image')) { $wpp_description = preg_replace('|<img[^><]+>|i', '', $wpp_description); } $pt_header = get_option('wpp_pt_head_site') ? '<a href="' . get_option('siteurl') . '" target="_blank" class="wpp_header_2">' . get_option('blogname') . '</a> <br/>' : ''; $pt_header .= get_option('wpp_pt_head_url') ? '<a href="' . get_permalink($post->ID) . '" target="_blank" class="wpp_header_3">' . get_permalink($post->ID) . '</a> <br />' : ''; $pt_header .= get_option('wpp_pt_head_date') ? get_option('wpp_ph_exp_date') . ': ' . date("D M j G:i:s Y / O ") . ' GMT<br />' : ''; $pt_header .= '<hr style="border:none; border-top: #DDDDDD dotted 1px;" />'; $pt_title = $print_title && get_option('wpp_pt_title') ? '<h2 style="text-align:' . get_option('wpp_save_text_align') . '">' . $print_title . '</h2><br>' : ''; $pt_links = $print_links && get_option('wpp_pt_links') ? '<strong>' . get_option('wpp_ph_links') . ':</strong> <ol type="1">' . $print_links . '</ol>' : ''; $pt_date = get_option('wpp_pt_date') ? get_option('wpp_ph_prod_date') . ': ' . $print_post_date . '<br>' : ''; $pt_md_date = get_option('wpp_pt_md') ? get_option('wpp_ph_prod_mod_date') . ': ' . $print_modified_date . '<br>' : ''; $pt_footer = get_option('wpp_pt_header') ? '<br>' . get_option('wpp_ph_exp_date') . ': ' . date("D M j G:i:s Y / O ") . ' GMT <br> ' . get_option('wpp_ph_page_exported_from') . ' ' . get_option('blogname') . ' [ <a href="' . str_replace(array('&wpp_export=print', '?wpp_export=print'), '', $_SERVER['REQUEST_URI']) . '" target="_blank">' . get_option('siteurl') . '</a> ]<hr/> Product Print by <a href="http://www.gvectors.com/?wpp" target="_blank">WooCommerce PDF & Print</a> plugin.' : ''; require WPP_FOLDER . 'template/header/print_head.php'; require WPP_FOLDER . 'template/print.php'; echo $print; } exit; }
function bulk_order_product_search() { // Query for suggestions $term = $_REQUEST['term']; $category = !empty($_REQUEST['category']) ? explode(',', $_REQUEST['category']) : array(); $excluded_products = !empty($_REQUEST['excluded']) ? explode(',', $_REQUEST['excluded']) : array(); $included_products = !empty($_REQUEST['included']) ? explode(',', $_REQUEST['included']) : array(); $search_by = isset($this->options['search_by']) ? $this->options['search_by'] : '4'; $max_items = isset($this->options['max_items']) ? $this->options['max_items'] : '-1'; $excluded_products = apply_filters('wc_bulk_order_excluded_products', $excluded_products); $included_products = apply_filters('wc_bulk_order_included_products', $included_products); if (empty($term)) { die; } if (!empty($category)) { if (is_numeric($term)) { if ($search_by == 2 || $search_by == 4) { $products1 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'post__in' => array(0, $term), 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); $products2 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'post_parent' => $term, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 3 || $search_by == 4) { $products4 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 's' => $term, 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1 || $search_by == 4) { $products3 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'meta_query' => array(array('key' => '_sku', 'value' => '^' . $term, 'compare' => 'REGEXP')), 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1) { $products = array_unique(array_merge(get_posts($products3))); } elseif ($search_by == 2) { $products = array_unique(array_merge(get_posts($products1), get_posts($products2))); } elseif ($search_by == 3) { $products = array_unique(array_merge(get_posts($products4))); } else { $products = array_unique(array_merge(get_posts($products1), get_posts($products2), get_posts($products3), get_posts($products4))); } } else { if ($search_by == 1 || $search_by == 4) { $products1 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'meta_query' => array(array('key' => '_sku', 'value' => '^' . $term, 'compare' => 'REGEXP')), 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 3 || $search_by == 4) { $products2 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 's' => $term, 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category)), 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1) { $products = array_unique(array_merge(get_posts($products1))); } elseif ($search_by == 3) { $products = array_unique(array_merge(get_posts($products2))); } else { $products = array_unique(array_merge(get_posts($products1), get_posts($products2))); } } } else { if (is_numeric($term)) { if ($search_by == 2 || $search_by == 4) { $products1 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'post__in' => array(0, $term), 'fields' => 'ids', 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); $products2 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'post_parent' => $term, 'fields' => 'ids', 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 3 || $search_by == 4) { $products4 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 's' => $term, 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1 || $search_by == 4) { $products3 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'meta_query' => array(array('key' => '_sku', 'value' => '^' . $term, 'compare' => 'REGEXP')), 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1) { $products = array_unique(array_merge(get_posts($products3))); } elseif ($search_by == 2) { $products = array_unique(array_merge(get_posts($products1), get_posts($products2))); } elseif ($search_by == 3) { $products = array_unique(array_merge(get_posts($products4))); } else { $products = array_unique(array_merge(get_posts($products1), get_posts($products2), get_posts($products3), get_posts($products4))); } } else { if ($search_by == 1 || $search_by == 4) { $products1 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 'meta_query' => array(array('key' => '_sku', 'value' => '^' . $term, 'compare' => 'REGEXP')), 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 3 || $search_by == 4) { $products2 = array('post_type' => array('product', 'product_variation'), 'post_status' => array('publish'), 'posts_per_page' => $max_items, 's' => $term, 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false); } if ($search_by == 1) { $products = array_unique(array_merge(get_posts($products1))); } elseif ($search_by == 3) { $products = array_unique(array_merge(get_posts($products2))); } else { $products = array_unique(array_merge(get_posts($products1), get_posts($products2))); } } } // JSON encode and echo // Initialise suggestions array global $post, $woocommerce, $product; $suggestions = ''; foreach ($products as $prod) { $hide_product = 'false'; $post_type = get_post_type($prod); $child_args = array('post_parent' => $prod, 'post_type' => 'product_variation'); $children = get_children($child_args); if ('product' == $post_type && empty($children)) { $product = wc_get_product($prod); $id = $product->id; $price_html = $product->get_price_html(); if (preg_match('/<ins>(.*?)<\\/ins>/', $price_html)) { preg_match('/<ins>(.*?)<\\/ins>/', $price_html, $matches); $price_html = $matches[1]; } $price_html = strip_tags($price_html); $price = $price_html; $sku = $product->get_sku(); $title = get_the_title($product->id); $title = html_entity_decode($title, ENT_COMPAT, 'UTF-8'); $img = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'thumbnail'); $img = $img[0]; if (!empty($img)) { $img = $img; } else { $img = apply_filters('woocommerce_placeholder_img_src', WC_Bulk_Order_Form_Compatibility::WC()->plugin_url() . '/assets/images/placeholder.png'); } } elseif ('product' == $post_type && !empty($children)) { $hide_product = 'true'; } elseif ('product_variation' == $post_type) { $product = new WC_Product_Variation($prod); $parent = wc_get_product($prod); $id = $product->variation_id; $price_html = $product->get_price_html(); if (preg_match('/<ins>(.*?)<\\/ins>/', $price_html)) { preg_match('/<ins>(.*?)<\\/ins>/', $price_html, $matches); $price_html = $matches[1]; } $price_html = strip_tags($price_html); $price = $price_html; $sku = $product->get_sku(); $title = $product->get_title(); $attributes = $product->get_variation_attributes(); $img = apply_filters('woocommerce_placeholder_img_src', WC_Bulk_Order_Form_Compatibility::WC()->plugin_url() . '/assets/images/placeholder.png'); foreach ($attributes as $name => $value) { $name = str_ireplace("attribute_", "", $name); $terms = get_the_terms($product->id, $name); foreach ($terms as $term) { if (strtolower($term->name) == $value) { $value = $term->name; } } $attr_name = $name; $attr_value = $value; $attr_value = str_replace('-', ' ', $value); if ($this->options['attribute_style'] === 'true') { $title .= ' ' . $attr_value . ' '; } else { if (strstr($attr_name, 'pa_')) { $atts = get_the_terms($parent->id, $attr_name); $attr_name_clean = WC_Bulk_Order_Form_Compatibility::wc_attribute_label($attr_name); } else { $np = explode("-", str_replace("attribute_", "", $attr_name)); $attr_name_clean = ucwords(implode(" ", $np)); } $attr_name_clean = str_replace("attribute_pa_", "", $attr_name_clean); $attr_name_clean = str_replace("Attribute_pa_", "", $attr_name_clean); $title .= " - " . $attr_name_clean . ": " . $attr_value; } $title = html_entity_decode($title, ENT_COMPAT, 'UTF-8'); } $parent_image = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'thumbnail'); $parent_image = $parent_image[0]; $img = wp_get_attachment_image_src(get_post_thumbnail_id($parent->id), 'thumbnail'); $img = $img[0]; if (!empty($img)) { $img = $img; } elseif (!empty($parent_image)) { $img = $parent_image; } else { $img = apply_filters('woocommerce_placeholder_img_src', WC_Bulk_Order_Form_Compatibility::WC()->plugin_url() . '/assets/images/placeholder.png'); } } if ($hide_product == 'false') { $symbol = get_woocommerce_currency_symbol(); $symbol = html_entity_decode($symbol, ENT_COMPAT, 'UTF-8'); $price = html_entity_decode($price, ENT_COMPAT, 'UTF-8'); // Initialise suggestion array $suggestion = array(); $switch_data = isset($this->options['search_format']) ? $this->options['search_format'] : '1'; $price = apply_filters('wc_bulk_order_form_price', $price, $product); switch ($switch_data) { case 1: if (!empty($sku)) { $label = $sku . ' - ' . $title . ' - ' . $price; } else { $label = $title . ' - ' . $price; } break; case 2: if (!empty($sku)) { $label = $title . ' - ' . $price . ' - ' . $sku; } else { $label = $title . ' - ' . $price; } break; case 3: $label = $title . ' - ' . $price; break; case 4: if (!empty($sku)) { $label = $title . ' - ' . $sku; } else { $label = $title; } break; case 5: $label = $title; break; } $suggestion['label'] = apply_filters('wc_bulk_order_form_label', $label, $price, $title, $sku, $symbol); $suggestion['price'] = $price; $suggestion['symbol'] = $symbol; $suggestion['id'] = $id; $suggestion['imgsrc'] = $img; if (!empty($variation_id)) { $suggestion['variation_id'] = $variation_id; } // Add suggestion to suggestions array $suggestions[] = $suggestion; } } // JSON encode and echo $response = $_GET["callback"] . "(" . json_encode($suggestions) . ")"; //print_r($response); echo $response; // Don't forget to exit! exit; }