【问题标题】:WooCommerce trigger URL after order complete订单完成后 WooCommerce 触发 URL
【发布时间】:2020-01-09 15:42:43
【问题描述】:

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

  • invite_email -> 这应该是下订单时使用的电子邮件
  • 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>';
        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, 'https://klantenvertellen.nl/v1/invite/external?hash=0926-4adb-a39e-d04110d1e445&location_id=10479&tenantId=99&invite_email='.$email.'&delay=1&first_name='.$voornaam.'&last_name='.$achternaam.'&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);
    }
}

当我使用它时,不会触发 URL。但是警报 URL 是这样的: https://klantenvertellen.nl/v1/invite/external?hash=0926-4adb-a39e-d04110d1e445&location_id=104679&tenantId=99&invite_email=test@email.com&delay=1&first_name=my名字&姓氏=我的姓氏&语言=nl

正如它应该的那样,但由于某种原因,CURL 函数没有被触发。当我在函数中添加这样的 URL 时:

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

URL 被触发。那么,当手动添加 URL 时它正在工作并且在使用变量时它不起作用,我做错了什么?

【问题讨论】:

    标签: php wordpress function woocommerce


    【解决方案1】:

    您可以像这样使用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);
        // ...
    }
    

    【讨论】:

    • 感谢您的回复,这让我找到了正确的方向。只有我不想在浏览器的链接中打开。我编辑了我的问题,如何在不在浏览器中打开网址的情况下使用 CURL 执行此操作?
    • @user2812779 是的,有可能,尝试使用 CURLOPT_RETURNTRANSFER:curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 我更新了我的答案。
    • 感谢您的回复。我几乎让它工作了,但由于某种原因,在函数内部的 functions.php 中它不起作用。你知道为什么吗?
    • @user2812779 没有看到functions.php文件,很难说!但您可以尝试以下操作:将您的函数和操作放入文件夹 wp-content/mu-plugins 中名为 woo-order-complete-actions.php 的文件中。如果此文件夹不存在,您可以创建它。它用于自定义“必须使用插件”并自动激活。在我看来,比使用与您的主题相关的functions.php 文件更容易。见WordPress doc on mu plugins
    • 再次非常感谢您的回答,我将功能更改为完成订购,以便更好地检查。我添加了一个警报,以便我可以检查我的动态 URL 是否正确。但是在函数中使用它时它不起作用。我编辑了我的答案。你知道我做错了什么吗?
    【解决方案2】:

    订单完成后,我终于在我的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 语句,因为我只想在客户从荷兰订购时触发它。问题出在名字或姓氏中的空格,所以我将它们替换为 %20 和 str_replace。

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

    【讨论】:

    • 嗨,我遇到了同样的事情,或者看起来,我有一个 page.php 并且需要在使用你的脚本后在有新订单时提醒该页面。如何接收在 page.php 上发送的数据
    猜你喜欢
    • 1970-01-01
    • 2015-07-10
    • 2013-08-18
    • 2020-03-15
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    相关资源
    最近更新 更多