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
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.
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).
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).
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.