Reputation: 11
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>';
}
});
Upvotes: 1
Views: 1041
Reputation: 11
Here's code that worked:
// 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>';
} elseif (!$product->is_in_stock() && !has_term('coming-soon', 'product_cat')) {
echo '<span class="now_sold">Sold Out</span>';
} elseif (!$product->is_in_stock() && has_term('coming-soon',
'product_cat')) {
echo '<span class="coming-soon">Coming Soon!</span>';
}
});
// Add WooCommerce New Badge for a Single Product
function filter_woocommerce_get_availability( $availability, $product ) {
if ( $product->is_featured() && !has_term( 'coming-soon', 'product_cat', $product->get_id() ) ) {
$availability['availability'] = __('NEW PRODUCT!', 'woocommerce' );
}
elseif ( ! $product->is_in_stock() && !has_term( 'coming-soon', 'product_cat', $product->get_id() ) ) {
$availability['availability'] = __('Sold Out', 'woocommerce' );
}
elseif ( ! $product->is_in_stock() && has_term( 'coming-soon', 'product_cat', $product->get_id() ) ) {
$availability['availability'] = __('Coming Soon', 'woocommerce' );
}
return $availability;
}
add_filter( 'woocommerce_get_availability', 'filter_woocommerce_get_availability', 10, 2 );
Upvotes: 0
Reputation: 23
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
// 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>';
} elseif (!$product->is_in_stock() && !has_term('coming-soon', 'product_cat')) {
echo '<span class="now_sold">Sold Out</span>';
} elseif (!$product->is_in_stock() && has_term('coming-soon', 'product_cat')) {
echo '<span class="now_sold">Coming Soon</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>';
} elseif (!$product->is_in_stock() && has_term('coming-soon', 'product_cat')) {
echo '<span class="now_sold">Coming Soon</span>';
} elseif (!$product->is_in_stock() && !has_term('coming-soon', 'product_cat')) {
echo '<span class="now_sold">Sold Out</span>';
}
});
Upvotes: 1