Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the stock quantity of an article from woocommerce?

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.

like image 647
Bill Bronson Avatar asked Nov 27 '13 13:11

Bill Bronson


2 Answers

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>
like image 150
Steve Avatar answered Oct 18 '22 21:10

Steve


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

like image 30
Sumith Harshan Avatar answered Oct 18 '22 21:10

Sumith Harshan