skip to Main Content

Is it possible to trigger an URL after an order is completed. It is not an URL the customer should be send to but in the back-end this URL should be visited so an API is called. I am a bit new to this so dont know if this is the right way. If someone could point me in the right direction that would be great.

I got this URL that should be triggered filled with variables I should get after completing the order:

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 -> This should be the e-mail the order is placed with
  • first_name -> This should be the name the order is placed with
  • last_name -> This should be the Last name the order is placed with
  • language -> This should be the language code (lowercase) of the shipping country the order is shipped to

I know this is a lot but how should I do this?

EDIT
Ok I found a way to do this with CURL but how can I excute below code without opening the URL in the browser?

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

I just want to excute the URL but dont want to open it in my browser. Just a quick call and that’s it.

Is this possible with CURL?

Edit 2.0.1

I changes my function with order status complete. I got this almost working but for some reason when using a “dynamic” url the URL is not triggered.

I also added an echo alert in javascript and this alert is passing the exact good dynamic URL so that is working as it shoud. Below the function with the alert:

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

When I use this the URL is not triggered. But the Alert URL is like this:
https://klantenvertellen.nl/v1/invite/external?hash=0926-4adb-a39e-d04110d1e445&location_id=104679&tenantId=99&[email protected]&delay=1&first_name=my first name&last_name=my lastname&language=nl

Exactly as it shoud but for some reason the CURL function is not triggered. When I add the URL like this in the function:

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

The URL is triggered. So what am I doing wrong that when adding the URL manualy it is working and when working with variables it aint working?

2

Answers


  1. Chosen as BEST ANSWER

    I finaly got it working with this function inside my functions.php when an order is completed.

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

    I also added a if statement because I only want to trigger it when a customer orderd from the Netherlands. The problem was with the spaces in the firstname or last name so I replaced them with %20 with a str_replace.

    Hope this can help other that want to do this.


  2. You could use the action woocommerce_payment_complete like so:

    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);
        // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search