Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter products from a specific custom meta data in Woocommerce shop page

I need to filter the WooCommerce shop page and only want to display products which expects a custom product meta data. This is what I've found in the archive-product.php:

/**
 * Hook: woocommerce_before_shop_loop.
 *
 * @hooked wc_print_notices - 10
 * @hooked woocommerce_result_count - 20
 * @hooked woocommerce_catalog_ordering - 30
 */
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
    while ( have_posts() ) {
        the_post();
        /**
         * Hook: woocommerce_shop_loop.
         *
         * @hooked WC_Structured_Data::generate_product_data() - 10
         */
        do_action( 'woocommerce_shop_loop' );
        wc_get_template_part( 'content', 'product' );
    }
}
woocommerce_product_loop_end();

So how can I pass filter values in this part to only show the products with meta key X and value Y?

Update

I've tried it the way Loic said but when I check more then one meta value it's causing problems and I can't see any products:

add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );
function show_only_products_with_specific_metakey( $meta_query, $query ) {
    // Only on shop pages
    if( ! is_shop() ) return $meta_query;

    $meta_query[] = array(
        'key'     => '_the_meta_key',
        'value'   => 'the_value',
        'compare' => 'EXIST'
    );

    //Don't works when adding the second one
    $meta_query[] = array(
        'key'     => '_the_meta_key',
        'value'   => 'the_value_2',
        'compare' => 'EXIST'
    );


    return $meta_query;
};

I've two products:

  • Product A -> Has the_value_2
  • Product B -> Has the_value

So I'm expecting these two products here. When I remove the second meta_query I'm getting only product B.

like image 898
Mr. Jo Avatar asked Oct 16 '22 09:10

Mr. Jo


1 Answers

You can use a custom function hooked in woocommerce_product_query_meta_query filter hook, where you will replace _the_meta_key in the code below, by your targeted meta_key:

add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );
function show_only_products_with_specific_metakey( $meta_query, $query ) {
    // Only on shop pages
    if( ! is_shop() ) return $meta_query;

    $meta_query[] = array(
        'key'     => '_the_meta_key',
        'compare' => 'EXISTS'
    );
    return $meta_query;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.


Addition (related to your last comment):

To make it work for multiple meta values you need to use 'compare' => 'IN', like:

add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );
function show_only_products_with_specific_metakey( $meta_query, $query ) {
    // Only on shop pages
    if( ! is_shop() ) return $meta_query;

    $meta_query[] = array(
        'key'     => '_the_meta_key',
        'value'     => array('L','XL'),
        'compare' => 'IN'
    );
    return $meta_query;
}

Code goes in the function.php file of your active child theme (or active theme). Tested and work.

WP meta_query documentation

like image 157
LoicTheAztec Avatar answered Oct 20 '22 21:10

LoicTheAztec