do_action after page has been built in wordpress

I added an action to woocommerce_thankyou that has a 45 second delay in it and it causes the page to hang for 45 seconds while it executes. I was wondering if there was some way hook this to an action that could fire after the woocommerce thankyou.php page has been built. I’ve already tried adding my own do_action at the end of the script but that didn’t work. The page still hangs for 45 seconds.

This is the action and its function…

Read More
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 that I’m doing this is because I am trying to place an automated phone call 45 seconds after the order is made.

Any help is greatly appreciated.

The shopping cart is here… http://www.eat365miami.com/lee-sushi/

Related posts

Leave a Reply

1 comment

  1. My solution was to hook into the wordpress footer and within the function of the action that I created, I used a woocommerce conditional tag that only outputs my ajax request if I am on the “order received” page. Here is the action that I created in my themes functions.php file…

    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 that others can find this useful 🙂