为了账号安全,请及时绑定邮箱和手机立即绑定

订单完成后 WooCommerce 触发 URL

订单完成后 WooCommerce 触发 URL

PHP
LEATH 2022-07-16 18:43:27
是否可以在订单完成后触发 URL。它不是客户应该发送到的 URL,但在后端应该访问此 URL,以便调用 API。我对此有点陌生,所以不知道这是否是正确的方法。如果有人能指出我正确的方向,那就太好了。我得到了这个应该被触发的 URL,其中填充了我在完成订单后应该得到的变量:https:///v1/invite/externalhash=XXX&location_id=XXX&tenantId=XX&invite_email=XXX&delay=X&first_name=XXX&last_name=XXX&language=XX&ref_code=XXXX邀请电子邮件 -> 这应该是下订单的电子邮件first_name -> 这应该是下订单的名称last_name -> 这应该是下订单的姓氏语言 -> 这应该是订单发货国家的语言代码(小写)我知道这很多,但我应该怎么做?编辑 好的,我找到了一种使用 CURL 执行此操作的方法,但是如何在不在浏览器中打开 URL 的情况下执行以下代码?// create a new cURL resource$ch = curl_init();// set URL and other appropriate optionscurl_setopt($ch, CURLOPT_URL, "http://www.example.com/");curl_setopt($ch, CURLOPT_HEADER, 0);// grab URL and pass it to the browsercurl_exec($ch);// close cURL resource, and free up system resourcescurl_close($ch);我只想执行 URL 但不想在我的浏览器中打开它。只是一个快速的电话,就是这样。CURL 可以做到这一点吗?编辑 2.0.1我在订单状态完成后更改了我的功能。我几乎可以正常工作,但由于某种原因,在使用“动态”网址时,不会触发网址。我还在 javascript 中添加了一个回显警报,这个警报传递了准确的动态 URL,因此它可以正常工作。在带有警报的功能下方:add_action('woocommerce_order_status_completed', 'custom_process_order');function custom_process_order($order_id) {    // Get the order info    $order = wc_get_order( $order_id );    if($order->get_billing_country() == "NL"){    // create a new cURL resourc        $ch = curl_init();        $voornaam = $order->get_billing_first_name();        $achternaam = $order->get_billing_last_name();        $email = $order->get_billing_email();        echo '<script type="text/javascript">alert("https://klantenvertellen.nl/v1/invite/external?hash=0926-4adb-a39e-d04110d1e445&location_id=104679&tenantId=99&invite_email='.$email.'&delay=1&first_name='.$voornaam.'&last_name='.$achternaam.'&language=nl");</script>';
查看完整描述

2 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

您可以woocommerce_payment_complete像这样使用该操作:


add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);

function custom_process_order($order_id) {

    // Get the order info

    $order = new WC_Order( $order_id );


    // Your custom logic here, for instance calling the url with curl

    $ch = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // ...

}


查看完整回答
反对 回复 2022-07-16
?
狐的传说

TA贡献1804条经验 获得超3个赞

完成订单后,我终于在我的functions.php中使用了这个函数。


add_action('woocommerce_order_status_completed', 'custom_process_order');

function custom_process_order($order_id) {

    // Get the order info

    $order = wc_get_order( $order_id );


    if($order->get_billing_country() == "NL"){

    // create a new cURL resourc

        $ch = curl_init();

        $voornaam = $order->get_billing_first_name();

            $voornaam_new = str_replace(' ', '%20', $voornaam);

        $achternaam = $order->get_billing_last_name();

            $achternaam_new = str_replace(' ', '%20', $achternaam);

        $email = $order->get_billing_email();


        // set URL and other appropriate options

        curl_setopt($ch, CURLOPT_URL, 'https://klantenvertellen.nl/v1/invite/external?hash=0926-4adb-a39e-d04110d1e445&location_id=104679&tenantId=99&invite_email='.$email.'&delay=1&first_name='.$voornaam_new.'&last_name='.$achternaam_new.'&language=nl');

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // grab URL and pass it to the browser

        curl_exec($ch);

        // close cURL resource, and free up system resources

        curl_close($ch);

    }

}

我还添加了一个 if 语句,因为我只想在客户从荷兰订购时触发它。问题在于名字或姓氏中的空格,因此我将它们替换为带有 str_replace 的 %20。


希望这可以帮助其他想要这样做的人。


查看完整回答
反对 回复 2022-07-16
  • 2 回答
  • 0 关注
  • 349 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号