Ecommerce Shopify WordPress Discussion

Search by ean in the woocommerce admin panel

I have a problem with entering a search by product ean number in the administrator panel. My code is: function custom_search_by_ean( $query ) { global $pagenow, $typenow; if ( is_admin() && $pagenow === 'edit.php' && $typenow === 'product' && isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) { $ean = $_GET['s']; $meta_query = array( 'relation' => 'OR', array( 'key' => '_ean', 'value' => $ean, 'compare' => 'LIKE' ) ); $query->set( 'meta_query', $meta_query ); error_log(print_r($query, true)); } } add_action( 'pre_get_posts', 'custom_search_by_ean' ); I changed the key to see if it would work. For example, key => '_sku' works. Does anyone have an idea what the problem is? I would like products to be searched in WooCommerce by ean, name, sku, id in the admin panel in the edit.php tab
I found a solution to this problem, I first extract the product ID that is searched for by EAN, and then using the 'post__in' parameter I get the result: function custom_product_search_by_ID_ean($search_term) { global $wpdb; $ean = sanitize_text_field($search_term); $results = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE p.post_type = 'product' AND p.post_status IN ('publish', 'private') AND pm.meta_key = '_ean' AND pm.meta_value LIKE '%%%s%%'", $ean ) ); if ($results) { $product_ids = wp_list_pluck($results, 'ID'); return $product_ids; } else { return array(); } } add_filter('pre_get_posts', 'custom_search_products_by_ean', 10); function custom_search_products_by_ean($q) { if (is_admin() && $q->is_main_query() && $pagenow === 'edit.php' && $typenow === 'product' && isset($_GET['s']) && !empty($_GET['s'])) { $ean_search = custom_product_search_by_ID_ean($_GET['s']); if (!empty($ean_search)) { $q->set('post__in', $ean_search); } } return $q; }

December 30, 2023

Your code is almost true but there are a couple of issues: The 'pre_get_posts' action is fired for every query on the page, not just the main query for editing products. You should check if it's the main query and if the query is for the 'product' post type. When searching for a product by EAN, you need to use the meta_query in a way that allows for partial matches since EANs are often only partially entered. Updated version of your code: function custom_search_by_ean($query) { global $pagenow, $typenow, $wpdb; if (is_admin() && $query->is_main_query() && $pagenow === 'edit.php' && $typenow === 'product' && isset($_GET['s']) && !empty($_GET['s'])) { $ean = $_GET['s']; $ean_meta_key = '_ean'; $query->query_vars['s'] = ''; $query->query_where .= $wpdb->prepare( " AND ($wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value LIKE %s)", $ean_meta_key, '%' . like_escape($ean) . '%' ); } } add_action('pre_get_posts', 'custom_search_by_ean'); Note: Make sure that the custom field storing the EAN is named '_ean' and that it contains the EAN data for the products.

December 30, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.