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

如何编写脚本自动将WordPress订单数据POST至Hanteo Chart服务器

解决方案:在WordPress内实现自动化订单数据同步

完全可以在WordPress/WooCommerce环境内完成需求,无需额外编程语言,用PHP编写自定义代码即可,具体步骤如下:

1. 编写核心处理函数

这个函数要完成三个核心动作:获取当日完成订单、提取指定字段、发送数据到Hanteo服务器。

关键细节说明:

  • 当日订单筛选:精准锁定date_completed在今日00:00至23:59之间的已完成订单
  • 字段提取:ean通常是WooCommerce产品的自定义元字段,需从产品对象中获取;其余字段可直接从订单商品项中提取
  • API请求:使用WordPress内置的wp_remote_post函数,避免依赖外部curl库,适配WP环境

代码示例:

function sync_hanteo_chart_orders() {
    // 基于站点时区定义当日时间范围
    $today_start = strtotime('today', current_time('timestamp'));
    $today_end = strtotime('tomorrow', current_time('timestamp')) - 1;

    // 查询当日完成的订单
    $orders = wc_get_orders(array(
        'status' => 'completed',
        'date_completed' => $today_start . '...' . $today_end,
        'limit' => -1, // 获取所有符合条件的订单
    ));

    $sync_data = array();

    foreach ($orders as $order) {
        // 遍历订单内的商品项
        foreach ($order->get_items() as $item) {
            $product = $item->get_product();
            // 提取指定字段
            $item_data = array(
                'ean' => $product->get_meta('ean', true), // 替换为你实际使用的自定义字段名
                'album_name' => $item->get_name(),
                'quantity' => $item->get_quantity(),
                'product_id' => $product->get_id(),
                'sku' => $product->get_sku(),
            );
            $sync_data[] = $item_data;
        }
    }

    // 有数据时发送到Hanteo服务器
    if (!empty($sync_data)) {
        $hanteo_response = wp_remote_post('https://your-hanteo-api-endpoint.com', array(
            'headers' => array(
                'Content-Type' => 'application/json',
                // 添加Hanteo要求的认证信息,比如API Key
                'Authorization' => 'Bearer YOUR_HANTEO_API_KEY',
            ),
            'body' => json_encode($sync_data),
            'timeout' => 30,
        ));

        // 记录日志用于调试
        if (is_wp_error($hanteo_response)) {
            error_log('Hanteo同步失败: ' . $hanteo_response->get_error_message());
        } else {
            error_log('Hanteo同步成功,响应码: ' . wp_remote_retrieve_response_code($hanteo_response));
        }
    }
}

2. 设置每日定时任务

利用WordPress的定时API,注册每日23点执行的同步任务,确保时区与站点后台设置一致。

代码示例:

function register_hanteo_sync_cron() {
    // 避免重复注册定时任务
    if (!wp_next_scheduled('hanteo_sync_event')) {
        // 设置每日23点执行,基于站点时区
        wp_schedule_event(strtotime('23:00:00'), 'daily', 'hanteo_sync_event');
    }
}
add_action('wp', 'register_hanteo_sync_cron');

// 将处理函数绑定到定时事件
add_action('hanteo_sync_event', 'sync_hanteo_chart_orders');

3. 关键注意事项

  • 自定义字段适配:如果你的ean字段元键不是ean,需替换为产品实际使用的字段名(可在WooCommerce产品编辑页的自定义字段中查看)
  • 认证调整:根据Hanteo API文档调整认证方式,比如API Key放在URL参数或其他请求头中
  • 时区校准:确保WordPress后台设置的时区正确,否则定时任务执行时间会出现偏差
  • 调试与日志:开启WordPress调试模式或查看服务器错误日志,排查同步失败问题
  • 性能优化:若当日订单量极大,可添加分页查询或异步处理逻辑,避免请求超时

无代码替代方案

如果不想编写代码,可借助第三方工具实现:

  • WP Crontrol:可视化管理WordPress定时任务
  • 无代码集成工具:通过Zapier/Make等平台连接WooCommerce与Hanteo API,配置每日同步规则(需第三方服务支持)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:55:12