简体   繁体   English

从产品类别中排除最近查看的产品小部件中的Woocommerce产品

[英]Exclude Woocommerce products in recently viewed products widget from a product category

I'm trying to figure out how to exclude product in a category from Recently Viewed Product Widget in Woocommerce. 我试图找出如何从Woocommerce的“最近查看的产品小部件”中排除类别中的产品。

I know products in a category can be removed/hidden from shop page using the below code 我知道可以使用以下代码从商店页面中删除/隐藏类别中的产品

function custom_pre_get_posts_query( $q ) {
    $tax_query = (array) $q->get( 'tax_query' );
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'machine' ), // Don't display products in the machine category on the shop page.
           'operator' => 'NOT IN'
    );
    $q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

I will like to know how to exclude products in "Machine Category" from showing up in recently viewed product widget. 我想知道如何从最近查看的产品小部件中排除“机器类别”中的产品。 (i'm using a search which auto-suggest products available on the store and it allows users to view product that are hidden from the archive page / category page), so i'd like to exclude the products from the recently viewed product widget if a user was able to access the product through the search. (我正在使用搜索功能来自动建议商店中提供的产品,并且它允许用户查看从存档页面/类别页面隐藏的产品),所以我想从最近查看的产品小部件中排除产品用户是否能够通过搜索访问产品。

I have used this code to exclude product in a category from displaying in search results, which works fine as expected but the issue is the auto suggestions which can still display product excluded/hidden from queries 我已使用此代码将类别中的产品排除在搜索结果中,从而按预期正常工作,但问题是自动建议仍然可以显示从查询中排除/隐藏的产品

function hello_pre_get_posts( $query ) {
   if ( $query->is_search() ) {
       $query->set( 'post_type', array( 'product' ) );
       $tax_query = array( array(
               'taxonomy' => 'product_cat',
               'field'   => 'slug',
               'terms'   => 'machine',
               'operator' => 'NOT IN',
           ),
       );
       $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'hello_pre_get_posts' );

Assistance on how to exclude viewed product from Recently Viewed Products Widget will be highly appreciated. 非常感谢您提供有关如何从“最近查看的产品”小部件中排除查看的产品的帮助。

You need to use woocommerce_recently_viewed_products_widget_query_args dedicated filter hook : 您需要使用woocommerce_recently_viewed_products_widget_query_args专用过滤器挂钩

// Exclude products in recently viewed products widget from "machine" product category
add_filter( 'woocommerce_recently_viewed_products_widget_query_args', 'custom_recently_viewed_products_widget_query_args', 10, 1 );
function custom_recently_viewed_products_widget_query_args( $args ) {

    $args['tax_query'][] = array(
           'taxonomy' => 'product_cat',
           'field'    => 'slug',
           'terms'    => array( 'machine' ), 
           'operator' => 'NOT IN', 
    );

    return $args;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM