Advertisement
lorro

WooCommerce - Number of products on sale

Nov 8th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  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.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement