如何显示 wooCommerce "即将推出"产品的特定徽章



我可以为"已售罄"或"新"的wooccommerce产品显示某些徽章,但我希望能够显示第三类-"即将推出">

到目前为止,我的代码如下。我正在努力瞄准既"缺货"又"即将上市"的产品。

有没有一种方法可以展示一款"缺货"而不是"即将上市"的产品?我不认为这是正确的-我不是一个编码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>';
}
});

我修改了您的代码并删除了不必要的代码。你们不需要对同一个钩子叫两次。

您可以使用has_term而不是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>';
}
});

以下是有效的代码:

// 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 );

最新更新