使用 Woocommerce 中的元查询从任何地方排除特定产品 [英] Exclude specific products from everywhere with a meta query in Woocommerce

查看:45
本文介绍了使用 Woocommerce 中的元查询从任何地方排除特定产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的商店页面和我的主页中排除给定城市的产品,我在主页上显示来自 flatsome UX Builder 的 woocommerce 商店小部件(不确定它是一个小部件)的产品.

I would like to exclude products from a given city from my shop page but also from my home page where I display products from the flatsome UX Builder's woocommerce shop widget (not sure it's a widget).

指定城市的产品没有出现在我的商店页面中,但它们仍然出现在我的主页中.

The product with the given city doesn't appear in my shop page, but they still appear in my home page.

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
    if ($q->is_main_query())
    {
        $meta_query = $q->get('meta_query');
        $meta_query[] = array(
            'key'=>'city',
            'value' => 'Cassis',
            'compare'=>'NOT EXISTS',
            );
        $q->set('meta_query',$meta_query);

        remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
    }
}

有什么想法吗?

推荐答案

在产品循环中使用 pre_get_posts 过滤器钩子代替 meta_query,您可以使用专用的 woocommerce_product_query_meta_query 过滤器钩子.

Instead of using pre_get_posts filter hook for a meta_query on products loop, you could use the dedicated woocommerce_product_query_meta_query filter hook.

现在对于您的问题,它可能是使用的小部件或短代码,因此还有一些专门的挂钩.

Now for your problem it could be a widgets or a shortcode that is used, so there is also some dedicated hooks for them.

由于 meta_query 将与 3 个挂钩函数相似,您可以在自定义函数中设置它并以这种方式在 3 个挂钩函数中调用它:

As the meta_query is going to be similar for the 3 hooked functions, you can set it in a custom function and call it in the 3 hooked functions this way:

// The meta query in a function
function custom_meta_query( $meta_query ){
    $meta_query[] = array(
        'key'=>'city',
        'value' => 'Cassis',
        'compare'=>'NOT EXISTS',
    );
    return $meta_query;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of the active child theme (or active theme).

这应该有效......

This should work…

这篇关于使用 Woocommerce 中的元查询从任何地方排除特定产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆