Sunday, April 14, 2019

Display Out of Stock Products on a separate page

Show out of stock products on a separate page.

Add the below PHP to your theme functions.php file (I always recommend to create a child theme).

Use the shortcode [out_of_stock_products] on any page you like.

Note – all products must be set to “Manage Stock”

1. How to create WooCommerce shortcode.
2. How to use a WordPress query to return those products that have 0 stock.


1:  /* Display All Out of Stock Products via a Shortcode - WooCommerce */  
2:  add_shortcode( 'out_of_stock_products', 'wpsohel_out_of_stock_products_shortcode' );  
3:  function wpsohel_out_of_stock_products_shortcode() {  
4:  global $product, $woocommerce, $woocommerce_loop;  
5:  $columns = 4;  
6:  $args = array(  
7:    'post_type'       => 'product',  
8:    'post_status'      => 'publish',  
9:    'meta_query'      => array(  
10:      array(  
11:        'key'      => '_stock',  
12:        'value'     => 1,  
13:        'compare'    => '<'  
14:      )  
15:    )  
16:  );  
17:  $loop = new WP_Query($args);  
18:  ob_start();  
19:  woocommerce_product_loop_start();  
20:  while ( $loop->have_posts() ) : $loop->the_post();  
21:  wc_get_template_part( 'content', 'product' );  
22:  endwhile;   
23:  woocommerce_product_loop_end();  
24:  woocommerce_reset_loop();  
25:  wp_reset_postdata();  
26:  return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';  
27:  }  

1 comment:

Blogger said...

If you're looking to lose weight then you certainly need to jump on this brand new personalized keto diet.

To create this keto diet service, certified nutritionists, fitness trainers, and professional cooks have united to develop keto meal plans that are productive, painless, price-efficient, and delightful.

Since their grand opening in 2019, thousands of people have already remodeled their figure and well-being with the benefits a great keto diet can offer.

Speaking of benefits: clicking this link, you'll discover eight scientifically-tested ones provided by the keto diet.