WooCommerce: display ONLY selling products in the store

I need to create a product archive page (usually a Store in WooCommerce ), but displays ONLY SALE products ONLY . Basically, it should use the same layout template as in archive-product.php . There will be a link in the main menu that will be directed to this page. How can I do it?

UPDATE

I managed to filter ON SALE products using the code below, located just above the line if ( have_posts() ) : ...

 $args = array( 'post_type' => 'product', 'order' => 'ASC', 'paged' => $paged, 'meta_query' => array( array( 'key' => '_sale_price', 'value' => 0, 'compare' => '>', 'type' => 'numeric' ) ) ); query_posts( $args ); 

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

However, this only works for simple products , and I need it to work for both Simple products and Variable .. p>

+10
wordpress product archive woocommerce products
source share
5 answers

@mirus' response to the short code gave me the idea of ​​checking how WooCommerce only requests products for sale. Obviously, WooCommerce has a function wc_get_product_ids_on_sale() , which will return the identifiers of the goods sold. Then we can easily customize the query using the post__in parameter to return only those specific elements.

WooCommerce has a woocommerce_product_query hook in class class-wc-query.php that allows us to modify a query before it starts ... it runs on pre_get_posts , which is the usual place to modify a query. Using a Woo hook means that you allow them to handle most 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 ); } 
+14
source share

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

 $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 it as a page template .

+9
source share

@gmaggio with 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; } 
+6
source share

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

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

0
source share

Solution for variables and simple products:

 add_action( 'save_post_product', 'update_product_set_sale_cat_var' ); function update_product_set_sale_cat_var( $post_id ) { $sales_ids = wc_get_product_ids_on_sale(); foreach ( $sales_ids as $sale_id ) : if ($sale_id == $post_id) : wp_set_object_terms($post_id, 'sale', 'product_cat', true ); else : if ( has_term( 'sale', 'product_cat', $post_id ) ) { wp_remove_object_terms( $post_id, 'sale', 'product_cat' ); } endif; endforeach; } 
0
source share

All Articles