WooCommerce: Display ONLY on-sale products in Shop

≯℡__Kan透↙ 提交于 2019-11-27 19:54:15

@mirus' answer regarding the shortcode gave me the idea to check out how WooCommerce is querying only the on-sale items. Apparently WooCommerce has a wc_get_product_ids_on_sale() function that will return the IDs of the on-sale items. Then we can easily adjust the query using the post__in parameter to only return those specific items.

WooCommerce has a woocommerce_product_query hook in the class-wc-query.php class that allows for us to modify the query before it is run.... it is run on pre_get_posts which is the usual place for modifying the query. Using Woo's hook just means you let them handle the majority of the conditional logic about when this query modification should be applied.

add_action( 'woocommerce_product_query', 'so_20990199_product_query' );

function so_20990199_product_query( $q ){

    $product_ids_on_sale = wc_get_product_ids_on_sale();

    $q->set( 'post__in', $product_ids_on_sale );

}

I managed to filter out the ON SALE products with the code below placed just above the if ( have_posts() ) : line...

$args = array(
    'post_type'      => 'product',
    'meta_query'     => array(
        'relation' => 'OR',
        array( // Simple products type
            'key'           => '_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        ),
        array( // Variable products type
            'key'           => '_min_variation_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        )
    )
);

query_posts( $args );

The code is placed in a copy of archive-product.php which I renamed archive-product_sale.php and made as a page template.

Mykola Mykolayovich Dolynskyi

@gmaggio using query_posts() will break your site. Use pre_get_posts

add_filter( 'pre_get_posts', 'catalog_filters' );
function catalog_filters( $query ) {
    if ( $query->is_main_query() && $query->post_type = 'product' ) {
        if(isset($_GET['onsale'])) {
            $meta_query = array(
                'relation' => 'OR',
                array( // Simple products type
                'key' => '_sale_price',
                'value' => 0,
                'compare' => '>',
                'type' => 'numeric'
                ),
                array( // Variable products type
                'key' => '_min_variation_sale_price',
                'value' => 0,
                'compare' => '>',
                'type' => 'numeric'
                )
            ); $query->set('meta_query', $meta_query); d($query);
        }
        if(isset($_GET['bestsellers'])) {
            $meta_query     = array(
            array( 
                'key'           => 'total_sales',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
                )
            );
        }
    }

return $query;
}

Create a new page using shortcode [sale_products per_page="12"]

List of available shortcodes and their parameters is here: http://docs.woothemes.com/document/woocommerce-shortcodes/

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