简体   繁体   English

在Woocommerce主页上的短代码中隐藏促销产品

[英]Hide on sale products from a shortcode on homepage in Woocommerce

I am trying to hide all the sale products on the homepage that is displaying products using Woocommerce shortcode. 我试图在使用Woocommerce简码显示产品的首页上隐藏所有销售产品。 I am new on here and after searching high and low, I couldn't find a solution. 我是这里的新手,在搜寻了高低之后,我找不到解决方案。

I have tried to use Hide all on sale products on Woocommerce shop page answer code and it works on shop page. 我试图在Woocommerce商店页面答案代码使用“ 隐藏所有销售产品”,并且它在商店页面上有效。

Is there a way for this code to be applied to the homepage instead of the shop page? 有没有办法将此代码应用于首页而不是商店页面?

I tried this slightly modified version: 我尝试了这个经过稍微修改的版本:

add_filter( 'woocommerce_product_query_meta_query', 'on_sale_products_not_in_archives', 10, 2 );
function on_sale_products_not_in_archives( $meta_query, $query ) {
    // For woocommerce shop pages
    if( is_page( 87 ) ){
        $meta_query[] = array(
            'key'     => '_sale_price',
            'value'   => '',
            'compare' => '=',
       );
    }
    return $meta_query;
}

But it didn't work. 但这没有用。

Any help on this is appreciated. 在这方面的任何帮助表示赞赏。

To filter the product query with shortcodes, you need to use woocommerce_shortcode_products_query filter hook, but this will work on all products except variable products. 要使用短代码过滤产品查询,您需要使用woocommerce_shortcode_products_query过滤器挂钩,但这将适用于除可变产品之外的所有产品。

The code (targeting home page only) : 代码(仅限定位首页)

add_filter( 'woocommerce_shortcode_products_query', 'hide_on_sale_products_in_home', 50, 3 );
function hide_on_sale_products_in_home( $query_args, $atts, $loop_name ){
    if( is_front_page() ){

        $query_args['meta_query'] = array( array(
            'key'     => '_sale_price',
            'value'   => '',
            'compare' => '=',
        ) );
    }
    return $query_args;
}

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

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

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