在 Woocommerce 中,我有一个名为restriction_id
。我想根据某些条件过滤产品限制 ID。例如,如果一个值设置为35
在 php 会话变量中,我想过滤掉任何将restriction_id 属性设置为的产品35
.
我会在这里放什么?
这是我的起始代码:
// Define the woocommerce_product_query callback
function action_woocommerce_product_query( $q, $instance ) {
// The code
};
// Add the action
add_action( 'woocommerce_product_query', __NAMESPACE__.'\\action_woocommerce_product_query', 10, 2 );
任何帮助表示赞赏。
更新:尝试以下操作税务查询 https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters反而:
add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $tax_query, $query ) {
if( is_admin() ) return $tax_query;
// HERE set the taxonomy of your product attribute (custom taxonomy)
$taxonomy = 'pa_restriction_id'; // Note: always start with "pa_" in Woocommerce
// HERE Define your product attribute Terms to be excluded
$terms = array( '35' ); // Note: can be a 'term_id', 'slug' or 'name'
// The tax query
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug', // can be a 'term_id', 'slug' or 'name'
'terms' => $terms,
'operator' => 'NOT IN', // Excluded
);
return $tax_query;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)