-
-
Save ashokdhaduk/0e16c716665ae7c1d42425787a854ea9 to your computer and use it in GitHub Desktop.
Show "Stock Out" if all variations are stock out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wc_show_variable_product_stock_out() | |
{ | |
global $product; | |
if( $product->is_type( 'variable' ) ) | |
{ | |
$args = array( | |
'post_type' => 'product_variation', | |
'post_status' => array( 'private', 'publish' ), | |
'numberposts' => -1, | |
'orderby' => 'menu_order', | |
'order' => 'asc', | |
'post_parent' => $product->id // $post->ID | |
); | |
$variations = get_posts( $args ); | |
$is_stock_out = true; | |
foreach ($variations as $key => $variation_post) | |
{ | |
$product1 = wc_get_product( $variation_post->ID ); | |
$stock = $product1->get_total_stock(); | |
if($product1->is_in_stock() && $stock > 0) | |
{ | |
$is_stock_out = false; | |
} | |
} | |
if($is_stock_out) | |
{ | |
?> | |
<p class="stock out-of-stock">Sold out</p> | |
<?php | |
} | |
} | |
} | |
add_action("woocommerce_single_product_summary", "wc_show_variable_product_stock_out", 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment