在 Woocommerce 3 中获取上次订阅续订订单信息

问题描述 投票:0回答:1

我们正在尝试将 woocommerce 订阅续订数据传递给忠诚度奖励计划,但遇到了各种问题,并且无法获取相关的 woocommerce 订阅信息或任何有用的信息。我们的 zinrelo 完整忠诚度代码可与手动数据配合使用。

包含您建议的完整代码在函数文件中运行

add_action( 'woocommerce_subscription_renewal_payment_complete', 'custom_add_subscription_points', 10, 1 );
  function custom_add_subscription_points( $subscription ) {
    if ( ! $subscription )
        return;

    // Get related orders
    $orders_ids = $subscription->get_related_orders();

    // Get the last renewal related Order ID
    $order_id = reset( $order_ids );

    $order = wc_get_order($order_id);
    $order_id = $order->get_id();
    $order_email = $order->get_billing_email();
    $order_date = $order->get_date_completed();
    $order_total = $order->get_total();
    $order_subtotal = $order->get_subtotal();

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://api.zinrelo.com/v1/loyalty/purchase");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "user_email={$order_email}&activity_id=made_a_purchase&order_{id=$order_id}&total={$order_total}&subtotal={$order_subtotal}");
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = "Partner-Id: 000000";
    $headers[] = "Api-Key: 000000";
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
}
php wordpress woocommerce orders woocommerce-subscriptions
1个回答
1
投票

由于

WC_Subscription
get_related_orders()
方法给出订单 ID 数组,您需要使用
end()
函数来获取最后一个订单 ID 并避免使用
wc_get_order()
函数期望唯一的错误订单 ID 作为参数 (但不是数组)

所以尝试一下:

add_action( 'woocommerce_subscription_renewal_payment_complete', 'custom_add_subscription_points', 10, 1 );
function custom_add_subscription_points( $subscription, $last_order ) {
    if ( ! $subscription )
        return;

    $order_email    = $last_order->get_billing_email();
    $order_date     = $last_order->get_date_completed();
    $order_total    = $last_order->get_total();
    $order_subtotal = $last_order->get_subtotal();
}

现在应该可以使用:

curl_setopt($ch, CURLOPT_POSTFIELDS, "user_email={$order_email}&activity_id=made_a_purchase&order_{id=$order_id}&total={$order_total}&subtotal={$order_subtotal}");
© www.soinside.com 2019 - 2024. All rights reserved.