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

时间:2019-02-21 18:12:44

标签: php wordpress woocommerce product taxonomy-terms

我正在尝试找出如何从Woocommerce的“最近查看的产品小部件”中排除类别中的产品。

我知道可以使用以下代码从商店页面删除/隐藏类别中的产品

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' );

我想知道如何从最近查看的产品小部件中排除“机器类别”中的产品。 (我正在使用搜索功能来自动建议商店中提供的产品,并且它允许用户查看从存档页面/类别页面隐藏的产品),所以我想从最近查看的产品小部件中排除产品用户是否可以通过搜索访问产品。

我已经使用此代码将类别中的产品排除在搜索结果中,这可以按预期工作,但是问题是自动建议仍然可以显示从查询中排除/隐藏的产品

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' );

我们将非常感谢您提供有关如何从“最近查看的产品”小部件中排除查看的产品的帮助。

1 个答案:

答案 0 :(得分:3)

您需要使用woocommerce_recently_viewed_products_widget_query_args dedicated filter hook

import Ember from 'ember';

export default Ember.Component.extend( {
didInsertElement(){
  Ember.$( '#bodyPage' ).on( 'onmousemove', this.onmouseMove.bind(this) );
},

onmouseMove(){
  var elmnt = document.getElementById( 'showImage' );

  document.onmousemove = function setStyle(e) {
    elmnt.setAttribute('style', `display:block`);
  }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

相关问题