Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

WordPress 监听所有订单状态变更

在 WooCommerce 中,监听订单状态变更并触发 API 可以通过添加动作钩子(action hooks)来实现。WooCommerce 提供了一系列针对订单状态变化的钩子,这些钩子允许您在订单状态发生变化时执行自定义代码。

监听所有订单状态变更

如果您想监听所有订单状态的变化,可以使用 woocommerce_order_status_changed 钩子。这个钩子在订单状态从一个状态变更为另一个状态时被触发。

add_action( 'woocommerce_order_status_changed', 'trigger_api_on_order_status_change', 10, 4 );

function trigger_api_on_order_status_change( $order_id, $old_status, $new_status, $order ) {
    $order = wc_get_order( $order_id );

    // 基本订单信息
    $api_data = array(
        'id' => $order->get_id(),
        'customer_id' => $order->get_customer_id(),
        'name' => $order->get_order_number(),
        'email' => $order->get_billing_email(),
        'phone' => $order->get_billing_phone(),
        'country_code' => $order->get_billing_country(),
        'cart_token' => '', // 如有需要填充
        'checkout_token' => '', // 如有需要填充
        'order_status' => 'PAID', // 根据实际情况修改
        'fulfillment_status' => 'UNFULFILLED', // 根据实际情况修改
        'currency' => $order->get_currency(),
        'total_discount' => $order->get_total_discount(),
        'subtotal_price' => $order->get_subtotal(),
        'total_shipping' => $order->get_total_shipping(),
        'total_tax' => $order->get_total_tax(),
        'total_price' => $order->get_total(),
        'payment_method' => $order->get_payment_method_title(),
        'payment_at' => $order->get_date_paid() ? $order->get_date_paid()->getTimestamp() : '',
        'shipping_method' => $order->get_shipping_method(),
        'updated_at' => $order->get_date_modified()->getTimestamp(),
        'created_at' => $order->get_date_created()->getTimestamp(),
        'order_status_url' => $order->get_view_order_url(),
    );

    // 添加地址信息
    $api_data['shipping_address'] = array(
        // ... 填充所有相关的地址字段
    );

    $api_data['billing_address'] = array(
        // ... 填充所有相关的地址字段
    );

    // 添加订单项
    $api_data['line_items'] = array();
    foreach ( $order->get_items() as $item_id => $item ) {
        $product = $item->get_product();
        $api_data['line_items'][] = array(
            'id' => $item_id,
            'src' => wp_get_attachment_image_src($product->get_image_id(), 'full')[0],
            'name' => $item->get_name(),
            'sku' => $product->get_sku(),
            'variant_title' => $product->get_title(),
            'variant_id' => $product->get_id(),
            'quantity' => $item->get_quantity(),
            'line_price' => $item->get_total(),
            'title' => $product->get_title(),
            'product_id' => $product->get_id(),
            'product_url' => get_permalink($product->get_id()),
            'fulfillment_status' => '', // 如有需要填充
            // ... 其他订单项字段
        );
    }

    // 发送API请求
    $api_url = '你的API端点';
    $response = wp_remote_post($api_url, array(
        'headers'     => array('Content-Type' => 'application/json; charset=utf-8'),
        'body'        => json_encode($api_data),
        'method'      => 'POST',
        'data_format' => 'body',
    ));

    // 处理响应
    // ...
}

监听特定订单状态变更

如果您只对特定的状态变更感兴趣,比如从任何状态变为“已完成”,可以使用特定状态的钩子,例如 woocommerce_order_status_completed

add_action( 'woocommerce_order_status_completed', 'trigger_api_when_order_completed', 10, 1 );

function trigger_api_when_order_completed( $order_id ) {
    $order = wc_get_order( $order_id );

    // 准备发送到API的数据
    $api_data = array(
        'order_id' => $order_id,
        'status' => 'completed',
        // ... 其他可能需要的数据
    );

    // 设置API URL
    $api_url = '您的API端点';

    // 发送API请求
    // ...
}

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论