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

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


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>


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


Simply add these lines in your single.php // your template for displaying he single postOr 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    }   }