当产品有货 WooCommerce 时显示自定义产品徽章
Display custom product badge when a product is back in stock WooCommerce
我知道如何为新创建的产品添加“新”徽章。这里我们使用 get_date_modified()
因为我们在创建产品时并不总是发布产品,并且希望它们也有 10 天的“新”徽章。
添加“新”徽章的工作解决方案:
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$created = strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxx.ch/wp-content/uploads/2021/01/new_badge.png"/></div>';
}
});
现在,有时产品缺货。在产品有货后,我们想显示一个徽章,如“Fresh back in stock”。但是对于get_date_modified()
的当前解决方案,由于get_date_modified()
函数,它总是会显示“新”徽章。
问题:
有没有一种方法可以让我只在产品从“缺货”到“有货”时显示徽章?
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$back_in_stock= strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $back_in_stock && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxxx.ch/wp-content/uploads/2021/01/back_in_stock_badge.png"/></div>';
}
});
您应该 add/update 在更新产品和产品库存时自定义产品元数据,如下所示:
if ($stock_qty >=1) {
update_post_meta( $product_id, '_earlier_update', true);
}
else{
update_post_meta( $product_id, '_earlier_update', false);
}
您可以相应地添加Fresh back in stock的条件。
如何add/update自定义产品元数据:
add_action( 'woocommerce_updated_product_stock', 'woo_updated_product_stock', 10, 3);
add_action( 'woocommerce_update_product', 'woo_updated_product_stock',10, 3 );
add_action('save_post_product', 'woo_updated_product_stock', 10, 3);
function woo_updated_product_stock( $product_id ) {
$product = wc_get_product( $product_id );
$stock_qty = $product->get_stock_quantity();
if ($stock_qty >=1) {
update_post_meta( $product_id, '_earlier_update', true);
}
else{
update_post_meta( $product_id, '_earlier_update', false);
}
}
当产品有货 WooCommerce 时显示自定义产品徽章
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$product_id = $product->get_id();
$date_modified = $product->get_date_modified()->date('Y-m-d');
// Add 10 day on product modify date becase fresh back in stock will show 10 days from current modify dates
$back_in_stock = strtotime($date_modified. " + $newness_days day");
$today = strtotime(date('Y-m-d'));
$earlier_update = get_post_meta( $product_id, '_earlier_update', true );
if (($earlier_update && $today < $back_in_stock) && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper">'.__('Fresh back in stock').'</div>';
}
});
您可以在每个产品中检查 $earlier_update,如果存在或为真,那么您可以在从上次修改日期到当前日期的 10 天内显示徽章
您可以相应地更改操作或条件。
我知道如何为新创建的产品添加“新”徽章。这里我们使用 get_date_modified()
因为我们在创建产品时并不总是发布产品,并且希望它们也有 10 天的“新”徽章。
添加“新”徽章的工作解决方案:
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$created = strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxx.ch/wp-content/uploads/2021/01/new_badge.png"/></div>';
}
});
现在,有时产品缺货。在产品有货后,我们想显示一个徽章,如“Fresh back in stock”。但是对于get_date_modified()
的当前解决方案,由于get_date_modified()
函数,它总是会显示“新”徽章。
问题: 有没有一种方法可以让我只在产品从“缺货”到“有货”时显示徽章?
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$back_in_stock= strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $back_in_stock && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxxx.ch/wp-content/uploads/2021/01/back_in_stock_badge.png"/></div>';
}
});
您应该 add/update 在更新产品和产品库存时自定义产品元数据,如下所示:
if ($stock_qty >=1) {
update_post_meta( $product_id, '_earlier_update', true);
}
else{
update_post_meta( $product_id, '_earlier_update', false);
}
您可以相应地添加Fresh back in stock的条件。
如何add/update自定义产品元数据:
add_action( 'woocommerce_updated_product_stock', 'woo_updated_product_stock', 10, 3);
add_action( 'woocommerce_update_product', 'woo_updated_product_stock',10, 3 );
add_action('save_post_product', 'woo_updated_product_stock', 10, 3);
function woo_updated_product_stock( $product_id ) {
$product = wc_get_product( $product_id );
$stock_qty = $product->get_stock_quantity();
if ($stock_qty >=1) {
update_post_meta( $product_id, '_earlier_update', true);
}
else{
update_post_meta( $product_id, '_earlier_update', false);
}
}
当产品有货 WooCommerce 时显示自定义产品徽章
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$product_id = $product->get_id();
$date_modified = $product->get_date_modified()->date('Y-m-d');
// Add 10 day on product modify date becase fresh back in stock will show 10 days from current modify dates
$back_in_stock = strtotime($date_modified. " + $newness_days day");
$today = strtotime(date('Y-m-d'));
$earlier_update = get_post_meta( $product_id, '_earlier_update', true );
if (($earlier_update && $today < $back_in_stock) && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper">'.__('Fresh back in stock').'</div>';
}
});
您可以在每个产品中检查 $earlier_update,如果存在或为真,那么您可以在从上次修改日期到当前日期的 10 天内显示徽章
您可以相应地更改操作或条件。