Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameemreza/ad3a611b77c56ca68ec305f23ce8a6e6 to your computer and use it in GitHub Desktop.
Save shameemreza/ad3a611b77c56ca68ec305f23ce8a6e6 to your computer and use it in GitHub Desktop.
//Hide the Bundle When a product is Out of Stock
add_filter( 'woocommerce_product_is_visible', 'hide_bundle_when_required_product_is_out_of_stock', 10, 2 );
function hide_bundle_when_required_product_is_out_of_stock( $visible, $product_id ) {
$product = wc_get_product( $product_id );
// Check if the product is a bundle
if ( $product && $product->is_type( 'bundle' ) ) {
$bundled_items = $product->get_bundled_items();
// Loop through all bundled items
foreach ( $bundled_items as $bundled_item ) {
// Check if it's a required product
if ( ! $bundled_item->is_optional() ) {
$bundled_product = $bundled_item->get_product();
// If the required product is out of stock, hide the bundle
if ( $bundled_product && ! $bundled_product->is_in_stock() ) {
return false;
}
}
}
}
return $visible;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment