WooCommerce后端属性的产品过滤器



我想为属性"brand"后端WooCommerce产品部分

我使用以下代码:

function custom_woocommerce_product_filters( $output ) {
global $wp_query;

$output .= wc_product_dropdown_categories(
array(
'show_option_none' => 'Filter by brand',
'taxonomy' => 'pa_brand',
'name' => 'pa_brand',
'selected' => isset( $wp_query->query_vars['pa_brand'] ) ? $wp_query->query_vars['pa_brand'] : ''
)
);

return $output;
}
add_filter( 'woocommerce_product_filters', 'custom_woocommerce_product_filters' );

这段代码显示了过滤器的下拉列表,其中包含所有品牌,但是单击" filter "不返回过滤后的产品。它只是简单地显示所有的产品。

有人能帮忙吗?

感谢

我最近发现你必须使用pre_get_posts()过滤器来过滤结果,仅仅有下拉列表是不够的。我在stackoverflow上搜索了类似的问题后得出的结论:

// Filter woocommerce admin products by artist
function apply_artist_custom_product_filters( $query ) {
global $pagenow;
// Ensure it is an edit.php admin page, the filter exists and has a value, and that it's the products page
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['pa_artist'] ) && $_GET['pa_artist'] != '' && $_GET['post_type'] == 'product' ) {
// Create meta query array and add to WP_Query
$meta_key_query = array(
array(
'taxonomy'     => 'pa_artist',
'field' => 'slug',
'terms'   => esc_attr( $_GET['pa_artist'] ),
'operator' => 'IN',
)
);
$query->set( 'tax_query', $meta_key_query );
}
}

add_action( 'pre_get_posts', 'apply_artist_custom_product_filters' );

完成大部分工作的代码片段在这里:WooCommerce:向Product Admin区域添加自定义过滤器。我只将元查询更改为税务查询。我不确定这是不是最好的方法,但对我来说很有效。我搞不懂的是"选定"。wc_product_dropdown_categories的一部分,对我来说,当我选择一个艺术家并点击滤镜,滤镜工作,但它不保存选择。

相关内容

最新更新