订单完成后 WooCommerce 触发 URL

是否可以在订单完成后触发 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 options

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");

curl_setopt($ch, CURLOPT_HEADER, 0);


// grab URL and pass it to the browser

curl_exec($ch);


// close cURL resource, and free up system resources

curl_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>';

LEATH
浏览 350回答 2
2回答

HUWWW

您可以woocommerce_payment_complete像这样使用该操作:add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);function custom_process_order($order_id) {&nbsp; &nbsp; // Get the order info&nbsp; &nbsp; $order = new WC_Order( $order_id );&nbsp; &nbsp; // Your custom logic here, for instance calling the url with curl&nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);&nbsp; &nbsp; // ...}

狐的传说

完成订单后,我终于在我的functions.php中使用了这个函数。add_action('woocommerce_order_status_completed', 'custom_process_order');function custom_process_order($order_id) {&nbsp; &nbsp; // Get the order info&nbsp; &nbsp; $order = wc_get_order( $order_id );&nbsp; &nbsp; if($order->get_billing_country() == "NL"){&nbsp; &nbsp; // create a new cURL resourc&nbsp; &nbsp; &nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; &nbsp; &nbsp; $voornaam = $order->get_billing_first_name();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $voornaam_new = str_replace(' ', '%20', $voornaam);&nbsp; &nbsp; &nbsp; &nbsp; $achternaam = $order->get_billing_last_name();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $achternaam_new = str_replace(' ', '%20', $achternaam);&nbsp; &nbsp; &nbsp; &nbsp; $email = $order->get_billing_email();&nbsp; &nbsp; &nbsp; &nbsp; // set URL and other appropriate options&nbsp; &nbsp; &nbsp; &nbsp; 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');&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_HEADER, 0);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&nbsp; &nbsp; &nbsp; &nbsp; // grab URL and pass it to the browser&nbsp; &nbsp; &nbsp; &nbsp; curl_exec($ch);&nbsp; &nbsp; &nbsp; &nbsp; // close cURL resource, and free up system resources&nbsp; &nbsp; &nbsp; &nbsp; curl_close($ch);&nbsp; &nbsp; }}我还添加了一个 if 语句,因为我只想在客户从荷兰订购时触发它。问题在于名字或姓氏中的空格,因此我将它们替换为带有 str_replace 的 %20。希望这可以帮助其他想要这样做的人。
打开App,查看更多内容
随时随地看视频慕课网APP