Skip to content

Instantly share code, notes, and snippets.

@ashokdhaduk
Forked from tanmay27vats/function.php
Created May 28, 2018 04:45
Show Gist options
  • Save ashokdhaduk/0e16c716665ae7c1d42425787a854ea9 to your computer and use it in GitHub Desktop.
Save ashokdhaduk/0e16c716665ae7c1d42425787a854ea9 to your computer and use it in GitHub Desktop.
Show "Stock Out" if all variations are stock out.
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