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

64,759

Solution 1

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>

Solution 2

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

Share:
64,759
Bill Bronson
Author by

Bill Bronson

Don't worry Be Happy In every life we have some trouble But when you worry you make it double Dont worry Be Happy Dont worry be happy now Dont worry Be happy Dont worry be happy Aint got no place to lay your head somebody came and took your bed Dont worry Be happy Your landlord say your rent was late He might have to Litigate dont worry (hahahahahaha) be happy (look at me I'm happy) Don't worry Be happy Hey i give you my phone number when you worried call me Illmake you happy Dont worry Be happy Aint got no cash aint got no style aint got no gal to make you smile but dont worry be happy Cause when you worry your face will frown And that will bring everybody down so Dont worry be happy Dont worry be happy now Dont worry Be happy Dont worry be happy Now there is song I wrote I hope you learned it note for note like good little children Dont worry Be happy Listen to what I say In your life expect some trouble But when you worry you make it double dont worry be happy be happy now! dont worry be happy dont worry be happy dont worry dont do it be happy put a smile on your face dont bring everybody down like that dont worry it will soon pass whatever it is dont worry be happy im not worried im happy

Updated on November 24, 2020

Comments

  • Bill Bronson
    Bill Bronson over 3 years

    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.

  • Steve
    Steve over 10 years
    Maybe you didnt see my edit, you should use $product->get_stock_quantity();
  • Bill Bronson
    Bill Bronson over 10 years
    ok it seems that im still doing something wrong. theres no errors anymore but the stock qty wont display... get_stock_quantity(get_the_ID());
  • Steve
    Steve over 10 years
    @BillBronson edited to clarify, dont pass in get_the_ID, it is a parameterless method.
  • Muhammad Bilal
    Muhammad Bilal about 10 years
    and how to check if stock is available?
  • Steve
    Steve about 10 years
    @MuhammadBilal if($product->get_stock_quantity() > 0){echo 'available';}
  • Jakob Runge
    Jakob Runge over 8 years
    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.
  • meer panhyar
    meer panhyar over 8 years
    to get the product quantity of a single product from woocommerce
  • pcarvalho
    pcarvalho almost 8 years
    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.