Php 基于自定义产品属性值筛选商业产品
在Woocommerce中,我有一个名为Php 基于自定义产品属性值筛选商业产品,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,在Woocommerce中,我有一个名为限制\u id的产品属性。我想根据某些限制id对产品进行筛选。例如,如果php会话变量中的值设置为35,我想过滤掉任何限制id属性设置为35的产品 我在这里放什么 这是我的起始代码: // Define the woocommerce_product_query callback function action_woocommerce_product_query( $q, $instance ) { // The code }; // Add
限制\u id
的产品属性。我想根据某些限制id对产品进行筛选。例如,如果php会话变量中的值设置为35
,我想过滤掉任何限制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 );
感谢您的帮助。更新:请尝试以下操作:
代码进入活动子主题(或活动主题)的function.php文件。已测试并正常工作。我无法使此代码正常工作。它省略了一切。我还尝试将运算符更改为“IN”,这也会忽略所有内容……限制id是自定义产品属性的名称。我所有的产品都是简单的产品。并非所有产品都有自定义限制id。目前我已设置了两个限制id为35的产品。我在woocommerce后端产品数据的属性部分下完成了这项工作,我称之为custom,因为当我添加一个属性以添加一个新属性时,它会显示“custom product attribute”,然后是add按钮。当您在测试服务器上尝试时,我将查看是否可以使用“标记”使其满足我的需要
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;
}