Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo woocommerce stock quantity of a particular product id

As the title says, how do i echo the stock quantity of a particular product id? Let's say the product id is "1100" and i want the stock quantity to appear in a page that is not part of the product archive. Is that possible?

For example, in an empty page, it should appear as

"Product A leftover stock: 10"

I am just learning to code and have come up with the following, but no result appear:

function nntest(){
    global $woocommerce;
    global $product;
    $product_id = 1100;
    echo $product->get_stock_quantity();
}

Thanks!

like image 421
zero Avatar asked Jul 24 '14 15:07

zero


People also ask

How do I get the quantity of a product in WooCommerce?

Or search for it in the plugins directory from inside your WordPress website (Plugins >> Add New) and install it. Then activate the plugin through the “Plugins” menu in WordPress. Finally, you can start using it directly at “WooCommerce > Settings > Product Quantity”.

How do I find product ID in WooCommerce product page?

A second option is to head over the Products page in your WordPress Admin. In this listing, you'll find the WooCommerce product ID when you hover over a product name. You can additionally search for your product using the product SKU name or product name and hover over the search results to get the Product ID.


1 Answers

You can use get_post_meta() function to get values from database.

this values are stored in wp_postmeta table.

$stock = get_post_meta( $post->ID, '_stock', true );
like image 185
Mauro Avatar answered Nov 07 '22 04:11

Mauro