Hide all on sale products on Woocommerce shop page

有些话、适合烂在心里 提交于 2019-12-24 10:28:26

问题


Would it be possible to hide all the products marked as "sale" on the standard WooCommerce shop page?

The sale products will be posted on a separate Sale page.


回答1:


To hide all on sale products (except variable products) on Woocommerce shop page, use:

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_shop() ){
        $meta_query[] = array(
            'key'     => '_sale_price',
            'value'   => '',
            'compare' => '=',
        );
    }
    return $meta_query;
}

Code goes in function.php file of your active child theme (or theme). Tested and works.


If you need that on all archives pages as product category archive pages and product archive tag pages, use:

if( is_shop() || is_product_tag() || is_product_category() ){

Instead of if( is_shop() ){

For variable products it's not possible as we need to check all the product variations of a variable product.



来源:https://stackoverflow.com/questions/49385446/hide-all-on-sale-products-on-woocommerce-shop-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!