使用 Woocommerce 和 Gravity Forms 产品附加组件完成付款后,将字段发送到 API

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

我已经禁用了所有默认的 woocommerce 计费和运输并且只创建了这两个字段电话号码电子邮件地址带有重力形式,所以这两个字段在Gravity Forms Product Add的帮助下显示在每个产品页面上-ons 供客户在订购产品时填写。

我曾经在我的旧站点上单独使用重力形式来执行此操作,在付款完成后执行下面的示例功能(仅使用重力形式挂钩 gform_post_payment_completed):

//Send to API
function gfroms_after_payment_sms( $entry, $action ) {

    // for more info, check https://www.gravityhelp.com/documentation/article/entry-object/

    $url = 'https://thirdparty.com/api/v2/send';
    $apiKey = '00000000000';
    $headers = array('api-key: '.$apiKey);
    $from = 'Sender's Name';
    $to = [$entry['14']]; //phone number field ID
    $email = $entry['10.1']; //Email field ID
    $body = "blah blah blah $email and $to from $from"; 
    
    
    $data = array (
       'recipients' => $to,
       'sender' => $from,
       'message' => $body
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $result = http_build_query($result, TRUE);
    curl_close($ch);
    
}
// Tells the notification to be sent only when this hook is found and to include the arguments ($entry and $action)
add_action( 'gform_post_payment_completed', 'gfroms_after_payment_sms',  10, 2 );

现在我如何使用 woocommerce hooks woocommerce_payment_complete 实现此目的我已经尝试了以下但它不起作用。

function custom_msg_customer_process_order( $order_id ) {

    $order = wc_get_order( $order_id );

    // Quit early if not a valid order.
    if ( ! $order ) {
         return;
    }

    $url = 'https://thirdparty.com/api/v2/send';
    $apiKey = '00000000000';
    $headers = array('api-key: '.$apiKey);
    $from = 'Sender's Name';
    $to = [$entry['14']]; //phone number field ID
    $email = $entry['10.1']; //Email field ID
    $body = "blah blah blah $email and $to from $from"; 


    $data = array (
       'recipients' => $to,
       'sender' => $from,
       'message' => $body
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $result = http_build_query($result, TRUE);
    curl_close($ch);


add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );

请问客户付款后我如何使用 woocommerce 进行这项工作,在此先感谢您的帮助。

wordpress woocommerce wordpress-rest-api gravity-forms-plugin gravityforms
1个回答
0
投票
add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );
function custom_msg_customer_process_order( $order_id ) {
    $order = wc_get_order( $order_id );
    // Quit early if not a valid order.
    if ( ! $order ) {
         return;
    }
    $cart_reg = $order->get_items();
    foreach( $cart_reg as $key => $value) { 
        $linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"]; 
        if (isset($linked_entry) && !empty($linked_entry)){
            $entry_id = $linked_entry;
            $entry = GFAPI::get_entry( $entry_id );
            if(!is_wp_error($entry){
              $url = 'https://thirdparty.com/api/v2/send';
              $apiKey = '00000000000';
              $headers = array('api-key: '.$apiKey);
              $from = 'Sender's Name';
              $to = [$entry['14']]; //phone number field ID
              $email = $entry['10.1']; //Email field ID
              $body = "blah blah blah $email and $to from $from"; 

              $data = array (
               'recipients' => $to,
               'sender' => $from,
               'message' => $body
              );

              $ch = curl_init();
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
              curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
              $result = curl_exec($ch);
              $result = http_build_query($result, TRUE);
              curl_close($ch);
            }
        }
    }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.