从 Woocommerce 商店页面中的特定自定义元数据中过滤产品 [英] Filter products from a specific custom meta data in Woocommerce shop page
问题描述
我需要过滤 WooCommerce 商店页面,并且只想显示需要自定义产品元数据的产品.这是我在 archive-product.php
中找到的:
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();
那么如何在这部分传递过滤器值以仅显示具有元键 X 和值 Y 的产品?
So how can I pass filter values in this part to only show the products with meta key X and value Y?
更新
我已经按照 Loic 所说的方式尝试过了,但是当我检查超过一个元值时,它会导致问题并且我看不到任何产品:
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;
};
我有两个产品:
- 产品 A -> 有
the_value_2
- 产品 B -> 有
the_value
所以我在这里期待这两种产品.当我删除第二个 meta_query
时,我只得到产品 B.
So I'm expecting these two products here. When I remove the second meta_query
I'm getting only product B.
推荐答案
您可以使用挂接在 woocommerce_product_query_meta_query
过滤器钩子中的自定义函数,您将在其中替换 _the_meta_key
下面的代码,按您的目标 meta_key
:
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;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.
Code goes in function.php file of your active child theme (or active theme). Tested and work.
添加(与您上一条评论有关):
要使其适用于多个元值,您需要使用 'compare' =>'IN'
,例如:
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;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.
Code goes in function.php file of your active child theme (or active theme). Tested and work.
这篇关于从 Woocommerce 商店页面中的特定自定义元数据中过滤产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!