【问题标题】:Woocommerce Excluding Certain Category from Shop PageWoocommerce 从商店页面中排除某些类别
【发布时间】:2017-01-11 21:27:06
【问题描述】:

试图从我的 WooCommerce 商店页面中排除单个类别

我正在使用此代码,但它会破坏我网站的属性过滤器链接:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    if ( ! is_admin() && is_shop() ) {

        $q->set( 'tax_query', array(array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
        )));

     }

     remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

【问题讨论】:

  • 我相信商店页面主要查询,它绝对是一个帖子类型的存档,所以那些会阻止它在商店存档上工作。
  • @CarlosDaniel ... 这意味着您应该删除 if ( ! $q->is_main_query() ) return;if ( ! $q->is_post_type_archive() ) return; ... :)

标签: php wordpress woocommerce


【解决方案1】:

此解决方案从商店页面中排除了该类别。但问题是,排除类别的类别存档页面显示没有可用的产品。

我不想在商店页面上加入电子书类别。我为 E-Books 创建了一个单独的菜单项,我想在其中列出 E-Books 类别中的所有书籍。

如何做到这一点?

更新

我在动作钩子中添加了if (!$q->is_main_query() || !is_shop()) return;,它解决了我上面提到的问题。此行仅从 Shop 页面中排除该类别,但是当直接从菜单(类别页面)访问排除的类别时,所有产品都会列出。

function custom_pre_get_posts_query( $q ) {

if (!$q->is_main_query() || !is_shop()) return;

$tax_query = (array) $q->get( 'tax_query' );

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


$q->set( 'tax_query', $tax_query );

}

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

【讨论】:

    【解决方案2】:

    您可以使用woocommerce_product_query 钩子,它与pre_get_posts 非常相似,只是它已经具有适当的条件逻辑。还有一个woocommerce_product_query_tax_query 过滤器,但我不确定它是否存在于 WooCommerce 2.6 中,或者它是否在 2.7 beta 中新出现。

    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
    
    function custom_pre_get_posts_query( $q ) {
    
        $q->set( 'tax_query', array(array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
        )));
    
    }
    

    EDIT 过滤是通过分类查询完成的,在上面的示例中,我们完全覆盖了税务查询。我现在无法测试这是否有效(数组数组很棘手,所以我可能搞砸了),但理论上我们需要将新约束与 WooCommerce 生成的现有分类查询合并。

    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
    
    function custom_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'samples' ), 
               'operator' => 'NOT IN'
        );
    
    
        $q->set( 'tax_query', $tax_query );
    
    }
    

    【讨论】:

    • 该解决方案可以从主页中排除该类别,但它不允许过滤这是我想要实现的目标。
    • 请查看编辑。请注意,如果您将过滤器小部件设置为“OR”关系,我认为您会发现小部件之间不可能存在 OR 关系,并且仍然与排除样本存在 AND 关系。不是不可能,但可能需要您编写自定义 posts_where SQL 语句,而不是编写分类查询。
    • 非常感谢,代码完美运行,如果我遇到任何错误,我会告诉你。还将过滤器小部件从“OR”更改为“AND”关系,以防止出现任何错误。你是一个救生员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2017-12-15
    • 2012-11-02
    • 2012-10-14
    • 2017-03-05
    • 1970-01-01
    • 2019-07-06
    相关资源
    最近更新 更多