daily pastebin goal
13%
SHARE
TWEET

WooCommerce - Number of products on sale

lorro Nov 8th, 2015 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // get the number of products in the shop which are on sale
  3.   // usage: We have [products_available] available!
  4.   // code goes in functions.php for your child theme
  5.   add_shortcode('products_available', 'products_available');
  6.   function products_available() {
  7.     $args = array(
  8.       'post_type' => 'product',
  9.       'posts_per_page' => -1,
  10.       'orderby' => '',
  11.       'order' => '',
  12.       'meta_query' => array(
  13.         array(
  14.         'key' => '_stock_status',
  15.         'value' => 'instock',
  16.         'compare' => '='
  17.         )
  18.       )
  19.     );
  20.     $loop = new WP_Query( $args );
  21.     $products = $loop->posts;
  22.     wp_reset_postdata();
  23.     return count($products);
  24.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top