WooCommerce: Display ONLY on-sale products in Shop

前端 未结 5 1370
花落未央
花落未央 2020-12-08 11:57

I need to create a products archive page (usually the Shop page in WooCommerce) but displays ONLY the ON SALE<

5条回答
  •  萌比男神i
    2020-12-08 12:23

    @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 );
    
    }
    

提交回复
热议问题