I have been googeling and trying different pieces of code to get the variation stock quantity to display on a product page.

So for example when I select XXL for a shirt it will display xx in stock.

I have been unable to find anything that works or has decent enough explanation for to recreate it.

I have tried: https://stackoverflow.com/questions/30514596/how-get-variations-stock-quantity-woocommerce and alot more.

Here is my current add to cart code:

 * Single variation cart button
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.4.0

defined( 'ABSPATH' ) || exit;

global $product;
<div class="woocommerce-variation-add-to-cart variations_button">
    <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

    do_action( 'woocommerce_before_add_to_cart_quantity' );

    woocommerce_quantity_input( array(
        'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
        'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
        'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
    ) );

    do_action( 'woocommerce_after_add_to_cart_quantity' );

    <button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

    <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

    <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="variation_id" class="variation_id" value="0" />

I have tried the below code but this only gives me 1 variable for XXXL and it shows the wrong stock amount.

        $product_variations = $product->get_available_variations();

        foreach ($product_variations as $variation)  {
            $var_data = $variation['attributes'];
            $var_data['in_stock'] = $variation['is_in_stock'];

        //List all attributes with stock available or not array..
        echo '<pre>';
        echo '</pre>';

I hope you guys can point me into the right direction!

Kind regards, Robbert

1 Answer 1


Displaying stock quantity per product variant is default WooCommerce behavior. For best results, when you develop own theme, use default storefront/woocommerce HTML structure and JS files then style it using CSS styles.

To test stock quantity per product variant is displaying correctly I installed clean WordPress 4.9.6 and WooCommerce 3.4.3 and followed Variable product page to add one.

Remember to set stock quantity per variant and add price to each variant to have a product in stock.

If you want to get stock quantity of each variant for some other reason you can use this code:

 * Get all variations stock quantity
 * @param int $product_id Product ID which has variations
function wpse_306996_get_variations_stock_quantity( $product_id ) {

    $product    = wc_get_product( $product_id );
    $variations = $product->get_available_variations();

    $variations_stock = array();

    foreach ( $variations as $variation ) {

        $variation_o = new WC_Product_Variation( $variation['variation_id'] );
        $variations_stock[] = $variation_o->get_stock_quantity();

    return $variations_stock;

$variations_stock = wpse_306996_get_variations_stock_quantity( $product_id );
  • WP 4.6 or 4.9.6? ;)
    – user2015
    Commented Jul 6, 2018 at 12:50
  • im guessing the above code goes into functions.php? Or how do I implement it? Im not building a custom theme BTW. I am trying to add it to a existing website/theme. Kind regards!
    – Granny
    Commented Jul 9, 2018 at 11:31
  • You should probably put this code in place where you want to display variants stock quantity.
    – kierzniak
    Commented Jul 9, 2018 at 15:58
  • Alright, so I marked your question with the bounty and the answer because I did not have time to check it the last few days and the bounty was running out and I did not know what would happen with it. I placed the code on the spot where I want to display the number and it outputs nothing. Not a single row.
    – Granny
    Commented Jul 11, 2018 at 6:49
  • I updated my answer to not work on actions. You can put wpse_306996_get_variations_stock_quantity function in functions.php file and move usage section to your template file. Remember to provide $product_id of product which has product variations.
    – kierzniak
    Commented Jul 11, 2018 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.