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

ぐ巨炮叔叔 提交于 2020-08-07 03:21:24

问题


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.


回答1:


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 function.php file of your active child theme (or active theme). Tested and work.

WP meta_query documentation




回答2:


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



来源:https://stackoverflow.com/questions/53631542/filter-products-from-a-specific-custom-meta-data-in-woocommerce-shop-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!