/** * Search for component options and echo json. * * @param string $x (default: '') * @param string $post_types (default: array('product')) * @return void */ public function json_search_component_options($x = 'default', $post_types = array('product')) { global $wpdb; ob_start(); check_ajax_referer('search-products', 'security'); $term = (string) wc_clean(stripslashes($_GET['term'])); $like_term = '%' . $wpdb->esc_like($term) . '%'; $composite_id = $_GET['composite_id']; $component_id = $_GET['component_id']; if (empty($term) || empty($composite_id) || empty($component_id)) { die; } $composite_data = get_post_meta($composite_id, '_bto_data', true); $component_data = isset($composite_data[$component_id]) ? $composite_data[$component_id] : false; if (false == $composite_data || false == $component_data) { die; } // Run query to get component option ids. $component_options = WC_CP()->api->get_component_options($component_data); // Add variation ids to component option ids. if ($x === 'search_component_options_in_scenario') { $variations_args = array('post_type' => array('product_variation'), 'post_status' => 'publish', 'posts_per_page' => -1, 'post_parent' => array_merge(array('0'), $component_options), 'fields' => 'ids'); $component_options_variations = get_posts($variations_args); $component_options = array_merge($component_options, $component_options_variations); } if (is_numeric($term)) { $query = $wpdb->prepare("\n\t\t\t\tSELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id\n\t\t\t\tWHERE posts.post_status = 'publish'\n\t\t\t\tAND (\n\t\t\t\t\tposts.post_parent = %s\n\t\t\t\t\tOR posts.ID = %s\n\t\t\t\t\tOR posts.post_title LIKE %s\n\t\t\t\t\tOR (\n\t\t\t\t\t\tpostmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t", $term, $term, $term, $like_term); } else { $query = $wpdb->prepare("\n\t\t\t\tSELECT ID FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id\n\t\t\t\tWHERE posts.post_status = 'publish'\n\t\t\t\tAND (\n\t\t\t\t\tposts.post_title LIKE %s\n\t\t\t\t\tor posts.post_content LIKE %s\n\t\t\t\t\tOR (\n\t\t\t\t\t\tpostmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t", $like_term, $like_term, $like_term); } $query .= " AND posts.post_type IN ('" . implode("','", array_map('esc_sql', $post_types)) . "')"; // Include results among component options only. $query .= " AND posts.ID IN (" . implode(',', array_map('intval', $component_options)) . ")"; // Include first 1000 results only. $query .= " LIMIT 1000"; $posts = array_unique($wpdb->get_col($query)); $found_products = array(); if ($posts) { foreach ($posts as $post) { $product = wc_get_product($post); if ($product->product_type === 'variation') { $found_products[$post] = WC_CP_Helpers::get_product_variation_title($product); } else { if ($x === 'search_component_options_in_scenario' && $product->product_type === 'variable') { $found_products[$post] = WC_CP_Helpers::get_product_title($product) . ' ' . __('— All Variations', 'woocommerce-composite-products'); } else { $found_products[$post] = WC_CP_Helpers::get_product_title($product); } } } } wp_send_json($found_products); }
/** * @deprecated 3.5.0 */ public function get_product_variation_title($variation_id) { _deprecated_function('WC_CP_API::get_product_variation_title()', '3.5.0', 'WC_CP_Helpers::get_product_variation_title()'); return WC_CP_Helpers::get_product_variation_title($variation_id); }