WooCommerce存储产品首次发布日期及自定义商品标签互斥显示咨询
WooCommerce 新品/补货标签互斥展示实现方案
以下是调整后的完整代码,可直接覆盖原有代码使用,完全满足提出的三个需求,同时保证两个标签不会同时出现:
// 商品首次发布时存储首次发布日期 function woo_first_product_published_date( $product_id ) { // 跳过自动保存 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; $product = wc_get_product($product_id); if (!$product) return; // 检查是否已经设置过首次发布日期 $first_publish_date = get_post_meta($product_id, '_first_publish_date', true); if (empty($first_publish_date)) { // 未设置则存储当前日期,格式为Unix时间戳方便计算 update_post_meta($product_id, '_first_publish_date', current_time('timestamp')); } } add_action('save_post_product', 'woo_first_product_published_date', 10, 1); // 库存更新时判断是否需要展示补货标签 function woo_updated_product_stock( $product_id ) { $product = wc_get_product( $product_id ); if (!$product) return; $stock_qty = $product->get_stock_quantity(); $first_publish_date = get_post_meta( $product_id, '_first_publish_date', true ); $newness_days = 10; $ten_days_ago = current_time('timestamp') - (60 * 60 * 24 * $newness_days); // 库存>=1且首次发布超过10天,设置补货标签标记为true if ($stock_qty >=1 && $first_publish_date < $ten_days_ago ) { update_post_meta( $product_id, '_earlier_update', true); } else { // 不满足条件则关闭补货标签标记 update_post_meta( $product_id, '_earlier_update', false); } } add_action( 'woocommerce_updated_product_stock', 'woo_updated_product_stock', 10, 1); add_action( 'woocommerce_update_product', 'woo_updated_product_stock',10, 1 ); add_action('save_post_product', 'woo_updated_product_stock', 10, 1 ); // 展示补货标签 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'); // 补货标签展示10天 $back_in_stock_expire = 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_expire) && !$product->is_on_sale() && $product->is_in_stock() ) { echo '<div class="badge_loop_wrapper"><img src="https://staging01.ch/wp-content/uploads/2021/09/back_in_stock_badge.png" alt="补货中"/></div>'; } }, 20 ); // 展示新品标签 add_action( 'woocommerce_before_shop_loop_item_title', function() { global $product; $newness_days = 10; $first_publish_date = get_post_meta( $product->get_id(), '_first_publish_date', true ); $ten_days_ago = current_time('timestamp') - (60 * 60 * 24 * $newness_days); // 首次发布10天内且未开启补货标记时展示新品标签 if ( $first_publish_date > $ten_days_ago && !$product->is_on_sale() && $product->is_in_stock() && !get_post_meta( $product->get_id(), '_earlier_update', true) ) { echo '<div class="badge_loop_wrapper"><img src="https://staging01.ch/wp-content/uploads/2021/01/new_badge.png" alt="新品"/></div>'; } }, 10 );
核心调整说明
- 补全了首次发布日期存储的逻辑,采用Unix时间戳存储方便后续日期对比计算
- 修正了首次发布日期距今超过10天的判断逻辑,避免原代码中日期格式不统一导致的判断错误
- 调整了两个标签钩子的优先级,同时在判断条件中增加互斥校验,保证不会同时出现两个标签
- 新品标签的判断依据从修改时间改为了自定义存储的首次发布时间,避免补货更新修改时间导致新品标签误判的问题
内容的提问来源于stack exchange,提问作者Nik7
相关产品推荐
相关产品推荐

