Mr. Jo
Mr. Jo

Reputation: 5281

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:

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

Upvotes: 5

Views: 12744

Answers (3)

Tayyab Chaudhary
Tayyab Chaudhary

Reputation: 508

Here is the complete workaround from my code:

add_action( 'woocommerce_product_query', 'mc_woocommerce_product_query_action', 10 );
function mc_woocommerce_product_query_action( $query ){
    $meta_query = $query->get( 'meta_query' );
    $tax_query = $query->get( 'tax_query' );

    if( !empty( $_GET['rate_type'] ) ) {
        $meta_query[] = array(
            'key'     => 'rate_type',
            'value'   => $_GET['rate_type'],
            'compare' => '=', // <=== Here you can set other comparison arguments
        );
    }

    if( !empty( $_GET['postcode'] ) ) {
        $meta_query[] = array(
            'key'     => 'auth_postcode',
            'value'   => $_GET['postcode'],
            'compare' => '=',
        );
    }

    if ( !empty( $_GET['service_type'] && $_GET['service_type'] !== '-1' ) ) {
        $tax_query[] = array(
            'taxonomy'  => 'product_cat',
            'field'     => 'term_id',
            'terms'     => array( $_GET['service_type'] ),
            // 'operator'  => 'IN'
        );
    }
    $query->set( 'meta_query', $meta_query );
    $query->set( 'tax_query', $tax_query );
    
    return $query;
}

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254378

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

Upvotes: 3

sorrow poetry
sorrow poetry

Reputation: 413

you can use do_action( 'woocommerce_shop_loop' );

function ctm_loop_53631963(){
    global $post;
    $meta = get_post_meta($post->ID, 'meta_value_key')
    if($meta !== 'desired_value'){
        continue;
    }
}

do_action('woocommerce_shop_loop', 'ctm_loop_53631963');

read more about wordpress actions Plugin API/Action Reference

Upvotes: 0

Related Questions