You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce如何自动为进口产品设置NEW IN分类并到期自动移除

问题排查

原代码存在以下问题:

  • 日期格式化逻辑错误:你将产品创建时间格式化为n/h/Y(月份/小时/年份),错误地把小时放到了日期的位置,会导致后续日期计算完全失效,无需额外格式化,可直接使用产品创建时间的DateTime对象
  • 天数统计错误:$difference->d仅返回日期间隔的「当月天数部分」,例如间隔65天的场景下d的值为5,要获取总间隔天数需使用$difference->days属性
  • 条件判断逻辑完全错误:使用了赋值运算符=而非比较运算符,且判断逻辑和需求相反,原代码逻辑是「创建时间超过60天才添加NEW IN分类」,和你的需求完全相反
  • 未处理移出分类的逻辑:没有实现满60天自动移出分类的功能
  • 缺少触发机制:未绑定WordPress钩子,代码不会自动执行,也没有兼容产品导入、批量更新的场景
  • 参数调用错误:wp_set_object_terms第一个参数需传入产品ID,而非产品对象
完整实现代码
// 定义NEW IN分类的ID,可根据实际情况修改
define('NEW_IN_CAT_ID', 40);

// 产品保存时自动更新NEW IN分类
add_action('save_post_product', 'update_new_in_category_for_product', 10, 2);
function update_new_in_category_for_product($post_id, $post) {
    // 跳过自动保存和修订版本
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
        return;
    }
    $product = wc_get_product($post_id);
    if (!$product) return;

    $created_date = $product->get_date_created();
    if (!$created_date) return;

    $now = new DateTime();
    $diff = $created_date->diff($now);
    $days_since_created = $diff->days;

    $product_cats = wp_get_post_terms($post_id, 'product_cat', ['fields' => 'ids']);

    if ($days_since_created < 60) {
        // 不足60天,添加NEW IN分类
        if (!in_array(NEW_IN_CAT_ID, $product_cats)) {
            wp_set_object_terms($post_id, NEW_IN_CAT_ID, 'product_cat', true);
        }
    } else {
        // 满60天,移除NEW IN分类
        if (in_array(NEW_IN_CAT_ID, $product_cats)) {
            $new_cats = array_diff($product_cats, [NEW_IN_CAT_ID]);
            wp_set_object_terms($post_id, $new_cats, 'product_cat');
        }
    }
}

// 注册每日定时任务,批量更新所有产品的NEW IN分类
add_action('wp', 'register_daily_new_in_update_cron');
function register_daily_new_in_update_cron() {
    if (!wp_next_scheduled('daily_update_new_in_category')) {
        wp_schedule_event(time(), 'daily', 'daily_update_new_in_category');
    }
}

add_action('daily_update_new_in_category', 'batch_update_all_products_new_in');
function batch_update_all_products_new_in() {
    $args = [
        'post_type' => 'product',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'fields' => 'ids'
    ];
    $product_ids = get_posts($args);
    foreach ($product_ids as $pid) {
        $product = wc_get_product($pid);
        if (!$product) continue;

        $created_date = $product->get_date_created();
        if (!$created_date) continue;

        $now = new DateTime();
        $diff = $created_date->diff($now);
        $days_since_created = $diff->days;

        $product_cats = wp_get_post_terms($pid, 'product_cat', ['fields' => 'ids']);

        if ($days_since_created < 60) {
            if (!in_array(NEW_IN_CAT_ID, $product_cats)) {
                wp_set_object_terms($pid, NEW_IN_CAT_ID, 'product_cat', true);
            }
        } else {
            if (in_array(NEW_IN_CAT_ID, $product_cats)) {
                $new_cats = array_diff($product_cats, [NEW_IN_CAT_ID]);
                wp_set_object_terms($pid, $new_cats, 'product_cat');
            }
        }
    }
}
使用说明
  • 代码放入当前主题的functions.php文件,或者自定义插件的代码文件中即可生效
  • 导入产品时会自动触发分类更新,无需额外操作
  • 每日定时任务会自动校验所有产品的创建时间,确保分类状态正确
  • 如果你不需要定时任务,删除对应代码段即可

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 01:54:05