• Resolved adw259

    (@adw259)


    I added an action to woocommerce_thankyou that has a 45 second delay in it and it causes the page to hang 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…

    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 = "3055478873"; // Calls must be made from a registered Twilio number.
    	$to_number = "3058345058";
    	$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,
    	    'https://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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The page won’t be sent to the browser until php has finished executing. The best you could do in php is to flush the output buffer which would send the page up to that point, then send the rest of the page 45 seconds later, but that’s not very good here.

    Try making Javascript do the waiting:

    <script type="text/javascript">
    setTimeout(function() {window.location = 'call.php';}, 45000);
    </script>

    call.php should not output any text otherwise it will overwrite the page.

    To get feedback from call.php you would need to use the xmlhttp object to call the page, (inside a setTimeout) read the response and output a suitable JavaScript alert. Its a steep learn but there’s a lot out there about how to do it:
    https://www.w3schools.com/xml/xml_http.asp

    Thread Starter adw259

    (@adw259)

    Thank you Iorro, using your advice as the basis moving forward, I eventually came up with this solution. See the stackoverflow link below…
    https://stackoverflow.com/questions/29808314/do-action-after-page-has-been-built-in-wordpress/29813428#29813428

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to execute do_action after page is loaded?’ is closed to new replies.