获取Woocommerce 3中的最新订阅续订订单信息

时间:2018-10-31 10:19:37

标签: php wordpress woocommerce orders woocommerce-subscriptions

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

1 个答案:

答案 0 :(得分:1)

由于WC_Subscription get_related_orders()方法提供了一个订单ID数组,因此您需要使用reset()函数来获取最新的续订订单ID,并避免wc_get_order()出错该功能需要一个唯一的订单ID作为参数(但不是数组)

所以尝试:

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 );

    // Get an instance of the WC_Order Object
    $order      = wc_get_order( $order_id );

    $order_email    = $order->get_billing_email();
    $order_date     = $order->get_date_completed();
    $order_total    = $order->get_total();
    $order_subtotal = $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}");