11

i got a little problem with displaying the stock quantity correctly.

heres the loop:

 <?php
 /**
 * Loop Price
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product;
?>

<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price">PREIS:<span class="amount"><?php echo $price_html; ?></span></span><p class="stock-m13"><?php get_sku(get_the_ID()); ?></p>
<?php endif; ?>

i want to show the user in the stock-m13 p the available quantity but im just gettin errors like "call to undefined function get_sku()".

what am i doing wrong? thx for any help.

Bill Bronson
  • 530
  • 2
  • 9
  • 24

3 Answers3

39

get_sku is a method of the product class, not a global function:

$product->get_sku()

Note that this will just get the stock code, not the actual quantity, perhaps you want:

$product->get_stock_quantity()

EDIT to clarify:

<p class="stock-m13"><?php echo $product->get_stock_quantity(); ?></p>
Steve
  • 20,703
  • 5
  • 41
  • 67
5

I'm using as following.

     <?php 
        global $product; 
        $numleft  = $product->get_stock_quantity(); 
        if($numleft==0) {
           // out of stock
            echo "There are no items available at this time."; 
        }
        else if($numleft==1) {
            echo "Only ".$numleft ." item left.";
        }
        else {
            echo "Only ".$numleft ." items left.";
        }
     ?>

Additional

Show total sold items.

     <?php 
       global $post;
       echo get_post_meta($post->ID, 'total_sales', true); 
     ?>

Hope this help. Thanks

Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
0

Simply add these lines in your single.php // your template for displaying he single post Or Id you want to display it on single product page

Simply dd these lines in single-product.php in your theme directory

 global $woocommerce;
 foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if( get_the_ID() == $_product->id ) {
       echo 'Quantity Is'. $values['quantity'];// quantity of the product
    } 
  } 
  • 1
    Hi and welcome to StackOverflow! It's always nice if you can put down some explanation to what your code does, so that others looking at it can get some understanding to what they're doing when they copy it. – Jakob Runge Oct 07 '15 at 10:35
  • 1
    to get the product quantity of a single product from woocommerce – meer panhyar Oct 07 '15 at 10:37
  • 2
    i know this is an old answer, but the question was to get the stock for a product, not the quantity of it, in the cart. – pcarvalho Jun 05 '16 at 04:19