繁体   English   中英

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

[英]Get the last subscription renewal order information in Woocommerce 3

我们正在尝试将 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);
}

由于WC_Subscription get_related_orders()方法提供了一个订单 ID 数组,您需要使用reset()函数来获取最后一个续订订单 ID 并避免wc_get_order()函数的错误, 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}");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM