Advertisement
lorro

WooCommerce - Show stock level in catalogue

Apr 23rd, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.48 KB | None | 0 0
  1. <?php
  2.   // WooCommerce hook to show stock level in the catalogue
  3.   add_action('woocommerce_after_shop_loop_item_title', 'show_stock', 15);
  4.   function show_stock() {
  5.     global $product;
  6.     $stock = intval($product->stock);
  7.     switch ($stock) {
  8.       case 0:
  9.         $text = 'Sold';
  10.         break;
  11.       case 1:
  12.         $text = 'Last one!';
  13.         break;
  14.       default:
  15.         $text = "Stock: ".$stock;
  16.     }
  17.     echo '<span class="show_stock">'.$text.'</span>'.PHP_EOL;
  18.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement