Get the total stock of all variations from a variable product In Woocommerce
Asked Answered
R

3

6

IN woocommerce I would like to display the total stock of all variations in a variable product. The stock of each product variation is managed at the product variation level.

If I use for a variable product:

global $product;

echo $product->get_stock_quantity();

I don't get the total stock of all variations of this product.

Is it possible to get the total stock of all variations from a variable product In Woocommerce?

Ramberg answered 17/10, 2018 at 14:19 Comment(1)
yes sir. i would like to display is in the single product page, catalog and search result page sirRamberg
S
10

The following custom function will displays the sum of all product variations stock quantity for a variable product (only).

It use a very light SQL query that make the sum of all children variations from a variable product.

Note: The stock need to be managed at product variation level.

function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
    global $wpdb, $product;

    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_id();

    // Check and get the instance of the WC_Product Object
    $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);

    // Only for variable product type
    if( $product->is_type('variable') ){

        // Get the stock quantity sum of all product variations (children)
        $stock_quantity = $wpdb->get_var("
            SELECT SUM(pm.meta_value)
            FROM {$wpdb->prefix}posts as p
            JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            WHERE p.post_type = 'product_variation'
            AND p.post_status = 'publish'
            AND p.post_parent = '$product_id'
            AND pm.meta_key = '_stock'
            AND pm.meta_value IS NOT NULL
        ");

        // Preparing formatted output
        if ( $stock_quantity > 0 ) {
            $html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
        } else {
            if ( is_numeric($stock_quantity) )
                $html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
            else
                $html = '';
        }

        // Different output options
        if( $output == 'echo_html' )
            echo $html;
        elseif( $output == 'return_html' )
            return $html;
        else
            return $stock_quantity;
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


USAGE (3 cases):

1) Anywhere in php code, get the stock quantity (from a dynamic $product_id):

$stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );

2) Display in shop and archives pages (Under the price range example):

add_action( 'woocommerce_after_shop_loop_item_title', 'display_variable_product_stock_quantity', 20 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
}

Code goes in function.php file of your active child theme (or active theme).

enter image description here


3) Display in single product pages (Under the price range example):

add_action( 'woocommerce_single_product_summary', 'display_variable_product_stock_quantity', 15 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
}

Code goes in function.php file of your active child theme (or active theme).

enter image description here

Stonemason answered 17/10, 2018 at 18:2 Comment(0)
R
2

This function returns the total stock quantity if the product is not a variable product or the stock management is enabled at product level.

Else it calculates the total stock for each variation.

You can place this function in for example functions.php. And call it like this:

<?php

global $product;
$stockQuantity = get_total_combined_stock_quantity($product);

?>
function get_total_combined_stock_quantity($product)
{
    if (!$product->is_type('variable')) {
        return $product->get_stock_quantity();
    }

    //Stock management is enabled at product level
    if ($product->managing_stock()) {
        return $product->get_stock_quantity();
    }

    $total = 0;

    if ($product->is_type('variable')) {
        foreach ($product->get_visible_children() as $variationId) {
            $variation = wc_get_product($variationId);
            $total += $variation->get_stock_quantity();
        }
    }

    return $total;
}
Returnee answered 19/5, 2022 at 13:38 Comment(0)
S
0
add_action( 'woocommerce_before_add_to_cart_button', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
    global $product;

    if ($product->is_type( 'variable' ))
    {
        $avail_vari = $product->get_available_variations();
        foreach ($avail_vari as $key => $value)
        {
            $vari_id = $value['variation_id'];
            $vari_colr = $value['attributes']['attribute_pa_colour'];
            $vari_obj = new WC_Product_variation($vari_id);
            $vari_stock = $vari_obj->get_stock_quantity();

            echo $vari_colr . ": " . $vari_stock . " ";

        }
    }

Paste this code into function.php of your theme and hurray your work will be done.

Saphead answered 1/7, 2020 at 15:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.