添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
function trigger_api_after_checkout( $order_id, $posted_data, $order ) {
    $order_data = $order->get_data(); // 获取订单数据
    // 收集客户信息
    $billing = $order_data['billing'];
    $shipping = $order_data['shipping'];
    // 准备API请求的数据
    $api_data = array(
        'id' => $order_id,
        'email' => $billing['email'],
        'phone' => $billing['phone'],
        'country_code' => $billing['country'],
        'sn' => $order_data['order_key'], // 使用订单键作为预生成订单编号
        'token' => '', // 根据您的系统生成或获取
        'cart_token' => WC()->session->get_customer_id(),
        'abandoned_checkout_url' => wc_get_checkout_url(),
        'status' => $order->get_status(),
        'currency' => $order_data['currency'],
        'total_price' => $order_data['total'],
        'created_at' => $order->get_date_created()->getTimestamp(),
        'updated_at' => $order->get_date_modified()->getTimestamp(),
        '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' => $item->get_variation_id() ? wc_get_formatted_variation(wc_get_product($item->get_variation_id()), true) : '',
            'variant_id' => $item->get_variation_id() ?: null,
            'quantity' => $item->get_quantity(),
            'price' => $item->get_subtotal(), // 或 $item->get_total()
            'line_price' => $item->get_total(),
            'product_id' => $product->get_id(),
            'product_url' => get_permalink($product->get_id()),
            'title' => $product->get_title(),
    // 发送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',
    // 处理响应
    // ...
add_action( 'woocommerce_checkout_order_processed', 'trigger_api_after_checkout', 10, 3 );