Redirect automatically from Woocommerce thankyou to an external link passing variables

丶灬走出姿态 提交于 2020-12-31 14:01:45

问题


In Woocommerce, after placing an order, I Would like to redirect customer automatically after 5 sec from the thankyou page to external link passing a few variables as the order_id, and the order_ammount.

So How can I redirect customer automatically from Woocommerce thankyou to an external link passing variables after 5 seconds?

Any track is welcome.


回答1:


The following code will redirect from checkout page to an external link passing few variables after 5 seconds using php and javascript:

 add_action( 'woocommerce_thankyou', 'thankyou_delated_external_redirection', 10, 1 );
function thankyou_delated_external_redirection( $order_id ){
    if( ! $order_id ){
        return;
    }

    $order          = wc_get_order( $order_id ); // Instannce of the WC_Order Object
    $order_total    = $order->get_total(); // Order total amount

    $link_redirect  = 'http://www.example.com/'; // Base url
    $link_redirect .= '?order_id='.$order_id.'&order_ammount='.$order_total; // passed variables

    ?>
    <script>
    jQuery(function($){
        // Redirect with a delay of 5 seconds
        setTimeout(function(){
            window.location.href = '<?php echo $link_redirect; ?>';
        }, 5000);
    });
    </script>
    <?php;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

The redirection link is like http://example.com/path/?order_id=1420&order_ammount=136.20



来源:https://stackoverflow.com/questions/55172245/redirect-automatically-from-woocommerce-thankyou-to-an-external-link-passing-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!