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

WooCommerce订单创建CPT时计算日差存入ACF数字字段实现求助

实现方案

前置准备

首先在ACF后台为自定义文章类型 groeiproces 新建一个数字类型字段,记录该字段的字段key(格式为field_xxxxxxxxxx,后续代码需替换为你实际的字段key)。

修改后的完整代码

// 订单完成后自动创建自定义文章
function create_post_after_order( $order_id ) {
    if ( $order_id instanceof WC_Order ){
        return;
    }

    $order = wc_get_order( $order_id );
    $order_items = $order->get_items(); 

    // 初始化存储商品数据的数组,避免未定义报错
    $product_ids = $product_names = $product_quantities = $ordeline_subtotals = $product_prices = array();

    // 循环遍历订单商品
    foreach ( $order_items as $item_id => $item_data ) {
        $product_ids[] = $item_data->get_product_id();
        $product_names[] = $item_data->get_name();
        $product_quantities[] = $item_data->get_quantity(); 
        $ordeline_subtotals[] = $item_data->get_subtotal(); 

        $product_details = $item_data->get_product();
        $product_prices[] = $product_details->get_price(); 
    }

    // 修复原代码$user_ID未定义问题
    $user_ID = get_current_user_id();
    // 创建自定义文章
    $new_post = array(
        'post_title' => "Order {$order_id}", 
        'post_date' => $order->get_date_created()->date('Y-m-d H:i:s'), // 优先使用订单实际创建时间,更符合业务需求
        'post_author' => $user_ID, 
        'post_type' => 'groeiproces', 
        'post_status' => 'publish',
    );    

    $post_id = wp_insert_post($new_post);

    // ========== 新增:计算间隔天数并写入ACF字段 ==========
    $post_publish_date = get_post_field( 'post_date', $post_id );
    $order_date = new DateTime( $post_publish_date );
    $current_date = new DateTime( current_time( 'mysql' ) ); // 采用WordPress带时区配置的当前时间,避免时区误差
    $date_diff = $order_date->diff( $current_date );
    $interval_days = $date_diff->days;

    // 请将下方field_xxxxxx替换为你自己新建的ACF数字字段的key
    $days_field_key = 'field_xxxxxx';
    update_field( $days_field_key, $interval_days, $post_id );
    // ========== 新增逻辑结束 ==========
    
    // ACF重复器字段配置
    $orderdetails_key = 'field_61645b866cbd6';
    $product_id_key = 'field_6166a67234fa3';
    $product_name_key = 'field_61645b916cbd7';
    $product_price_key = 'field_6166a68134fa4';
    $product_quantity_key = 'field_6165bd2101987';
    $ordeline_subtotal_key = 'field_6166a68934fa5';

    $orderdetails_value = array();
    // 保存订单商品数据到ACF重复器
    foreach ($product_ids as $index => $pid) {
        $orderdetails_value[] = array(
            $product_id_key => $pid, 
            $product_name_key => $product_names[$index],
            $product_price_key => $product_prices[$index],
            $product_quantity_key => $product_quantities[$index],
            $ordeline_subtotal_key => $ordeline_subtotals[$index],
        );
    }
    // 把update_field移到循环外,避免重复执行多次,提升性能
    update_field( $orderdetails_key, $orderdetails_value, $post_id );
}
add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

补充说明

上述代码仅会在订单创建生成文章时计算一次间隔天数,如果你需要间隔天数随时间自动更新,可以额外添加WordPress定时任务,每天批量更新所有groeiproces类型文章的天数字段即可。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:15:01