skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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;
    }
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search