php - 当产品有货时显示自定义产品徽章 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>';
}
});
解决方案
您应该在更新产品和产品库存时添加/更新自定义产品元数据,如下所示:
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_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 天内显示徽章
您可以相应地更改操作或条件。
推荐阅读
- reporting-services - 使用雪花数据源在 SSRS 上传递数据参数
- pip - 在 Windows 环境中复制串行通信
- arrays - 如何在这个数组中找到第 k 个项
- spring - Activiti:仅运行与特定流程定义 ID 关联的计时器作业
- javascript - 为什么我的 XMLHttpRequest 在 PHP 文件上传时中止?
- kotlin - 如何在 mutableStateOf() 中存储一对值
- sql - 如何构造一个只有一行列的行文字?
- json - 没有找到模拟器作为 `emulator -list-avds` 的输出
- python - [Python3][matplotlib] 奇怪的图形问题
- azure - azure 中是否有等效于 aws 终止处理程序的处理程序?