Do_action after the page has been built in wordpress

I added an action on woocommerce_thankyou

that has a 45 second delay in it and this causes the page to hang for 45 seconds while it is executing. I was wondering if there would be some way to link this to an action that might fire after the page is created woocommerce thankyou.php

. I've already tried adding my own do_action

at the end of the script, but it didn't work. The page is still hanging for 45 seconds.

This action and its function ...

add_action('woocommerce_thankyou', 'call_restaurant');

function call_restaurant() {
    sleep(45);
    require_once '/home/mywebsite/public_html/voice/Services/call.php';

    $sid = "bla bla bla";
    $token = "bla bla bla";

    $from_number = "3055551234"; // Calls must be made from a registered Twilio number.
    $to_number = "3055551234";
    $message = "Hello. You have received a new order from eat three sixty five miami dot com";

    $client = new Services_Twilio($sid, $token, "2010-04-01");
    /*
    $call = $client->account->calls->create(
        $from_number,
        $to_number,
        'http://twimlets.com/message?Message='.urlencode($message)
    );
    */
    echo 'phone call has been made';
}

      

The reason I am doing this is because I am trying to make an automatic phone call 45 seconds after the order was created.

Any help is greatly appreciated.

Cart here ... http://www.eat365miami.com/lee-sushi/

+3


source to share


1 answer


My solution was to connect to the wordpress footer and within the function I created, I used a conditional woocommerce tag that only outputs my ajax request if I am on the Order Received page. Here is the action I created in my themes functions.php files ...

add_action ('wp_footer', 'print_call_restaurant_javascript');

function print_call_restaurant_javascript () {

    if (is_wc_endpoint_url ('order-received')) {
        echo '  
        

            var xmlhttp;

            if (window.XMLHttpRequest) {
                // code for IE7 +, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest ();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == XmlHttpRequest.DONE) {
                   if (xmlhttp.status == 200) {
                       document.getElementById ("myDiv"). innerHTML = xmlhttp.responseText;
                   }
                   else if (xmlhttp.status == 400) {
                      alert (\ 'There was an error 400 \');
                   }
                   else {
                       alert (\ 'something else other than 200 was returned \');
                   }
                }
            }

            xmlhttp.open ("GET", "http://eat365miami.com/voice/call_restaurant.php?order_number=123", true);
            xmlhttp.send ();

        ';
    }

}



I hope others can find this useful :)

+1


source







All Articles