I’m able to display certain badges for woocommerce products that are either ‘sold out’, or ‘new’, but I want to be able to show a third category – ‘coming soon’
my code so far is below. I’m trying to target products that are both ‘out of stock’ AND category ‘coming-soon’.
Is there a way to show a product that is ‘out of stock’ and NOT ‘coming soon’? I dont think this is correct – I’m not a coder lol: !is_product_category
// Add WooCommerce Sold Out Badge Storewide
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
if ( ! $product->is_in_stock() && !is_product_category( 'coming-soon' ) ) {
echo '<span class="now_sold">Sold Out</span>';
}
});
// Add WooCommerce Sold Out Badge for a Single Product
add_action( 'woocommerce_before_single_product_summary', function() {
global $product;
if ( ! $product->is_in_stock() && !is_product_category( 'coming-soon' ) ) {
echo '<span class="now_sold">Sold Out</span>';
}
});
// Add WooCommerce New Badge Storewide
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
if ( $product->is_featured() ) {
echo '<span class="new">New</span>';
}
});
// Add WooCommerce New Badge for a Single Product
add_action( 'woocommerce_before_single_product_summary', function() {
global $product;
if ( $product->is_featured() ) {
echo '<span class="new">New</span>';
}
});
// Add WooCommerce Coming Soon Badge Storewide
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
if ( ! $product->is_in_stock() && is_product_category( 'coming-soon' ) ) {
echo '<span class="now_sold">Coming Soon</span>';
}
});
// Add WooCommerce Coming Soon Badge for a Single Product
add_action( 'woocommerce_before_single_product_summary', function() {
global $product;
if ( ! $product->is_in_stock() && is_product_category( 'coming-soon' ) ) {
echo '<span class="now_sold">Coming Soon</span>';
}
});
2
Answers
Here's code that worked:
I modified your code and remove unnecessary code . You don’t need to call same hook’s twice .
You may use has_term instead of is_product_category