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

创建Cron任务更新WooCommerce商品库存状态失败求助

问题分析与修复方案

原代码核心问题

  1. Cron触发无参数传递:函数定义了$post_id参数,但Cron事件触发时未传递任何值,导致无法处理商品
  2. 日期格式错误:date('YYYY-mm-dd')是无效格式,PHP日期格式化应使用Y-m-d
  3. 未初始化产品对象:直接调用$product->get_meta()时$product未定义,会触发致命错误
  4. 分类判断函数误用:is_product_category()是前端页面条件判断函数,Cron环境下无法用于检查商品所属分类
  5. 库存更新方式不规范:直接修改_stock等post meta,未使用WooCommerce官方API,可能导致数据不一致
  6. 未遍历目标商品:函数仅针对单个商品设计,但未批量查询所有符合条件的预订单商品

修正后的完整代码

// Custom CRON Function
function update_pre_order_stock_status() {
    // 定义目标预订单分类slug数组
    $target_categories = array( 
        'comic-book-pre-orders', 
        'dc-comics-pre-orders', 
        'image-comics-pre-orders', 
        'manga-pre-orders', 
        'marvel-comics-pre-orders', 
        'other-publisher-pre-orders'
    );

    // 查询所有目标分类下的已发布商品
    $product_query = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $target_categories,
                'operator' => 'IN'
            )
        )
    );

    $products = get_posts( $product_query );

    // 遍历处理每个商品
    foreach ( $products as $product_post ) {
        // 初始化WooCommerce产品对象
        $product = wc_get_product( $product_post->ID );
        if ( ! $product ) continue;

        // 获取自定义元数据
        $expiry_date = $product->get_meta( 'woo_expiry_date' );
        $expiry_action = $product->get_meta( 'woo_expiry_action' );

        // 跳过无过期日期或动作不为'out'的商品
        if ( empty( $expiry_date ) || $expiry_action !== 'out' ) continue;

        // 格式化日期为统一可比较格式
        $current_date = date( 'Y-m-d' );
        $formatted_expiry_date = date( 'Y-m-d', strtotime( $expiry_date ) );

        // 检查日期是否过期
        if ( $formatted_expiry_date <= $current_date ) {
            // 使用WooCommerce官方方法更新库存
            $product->set_stock_quantity( 0 );
            $product->set_stock_status( 'outofstock' );
            // 添加缺货可见性标记
            wp_set_post_terms( $product_post->ID, 'outofstock', 'product_visibility', true );
            // 保存产品变更
            $product->save();
        }
    }
}

// 调度Cron事件(每小时执行一次)
if ( ! wp_next_scheduled( 'my_pre_order_stock_update' ) ) {
    wp_schedule_event( time(), 'hourly', 'my_pre_order_stock_update' );
}

add_action( 'my_pre_order_stock_update', 'update_pre_order_stock_status' );

调试建议

  • 临时缩短执行间隔:如果需要快速测试,可以添加自定义Cron间隔(比如每分钟执行),测试完成后改回 hourly
  • 添加日志记录:在函数中加入error_log( 'Processed product: ' . $product_post->ID );,通过服务器日志查看函数是否正常执行
  • 验证元数据格式:确保woo_expiry_date存储的是可被strtotime()解析的日期格式(如2024-05-20)
  • 检查Cron运行状态:使用WP Crontrol等插件验证my_pre_order_stock_update事件是否正常触发

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 17:37:29