[英]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.我需要过滤 WooCommerce 商店页面,并且只想显示需要自定义产品元数据的产品。 This is what I've found in the archive-product.php
:这是我在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?那么如何在这部分中传递过滤器值以仅显示具有元键 X 和值 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:我已经按照 Loic 所说的方式进行了尝试,但是当我检查多个元值时,它会导致问题并且我看不到任何产品:
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:我有两个产品:
the_value_2
产品 A -> 具有the_value_2
the_value
产品 B -> 具有the_value
So I'm expecting these two products here.所以我在这里期待这两种产品。 When I remove the second meta_query
I'm getting only product B.当我删除第二个meta_query
时,我只得到产品 B。
I need to filter the WooCommerce shop page and only want to display products which expects a custom product meta data.我需要过滤WooCommerce商店页面,只希望显示需要自定义产品元数据的产品。 This is what I've found in the archive-product.php
:这是我在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?那么,如何在此部分中传递过滤器值以仅显示具有元键X和值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:我已经按照Loic所说的方法进行了尝试,但是当我检查了一个以上的meta值时,就会引起问题,并且看不到任何产品:
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:我有两种产品:
the_value_2
产品A->具有the_value_2
the_value
产品B->具有the_value
So I'm expecting these two products here.因此,我期望这里有这两种产品。 When I remove the second meta_query
I'm getting only product B.当我删除第二个meta_query
我只得到产品B。
I need to filter the WooCommerce shop page and only want to display products which expects a custom product meta data.我需要过滤WooCommerce商店页面,只希望显示需要自定义产品元数据的产品。 This is what I've found in the archive-product.php
:这是我在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?那么,如何在此部分中传递过滤器值以仅显示具有元键X和值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:我已经按照Loic所说的方法进行了尝试,但是当我检查了一个以上的meta值时,就会引起问题,并且看不到任何产品:
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:我有两种产品:
the_value_2
产品A->具有the_value_2
the_value
产品B->具有the_value
So I'm expecting these two products here.因此,我期望这里有这两种产品。 When I remove the second meta_query
I'm getting only product B.当我删除第二个meta_query
我只得到产品B。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.