Z
Z
zeaovede2022-04-11 11:26:13
JavaScript
zeaovede, 2022-04-11 11:26:13

How to redirect woocommerce with order-pay?

When trying to pay through the payment system, it sends to the order_pay page.
6253e4f77039f723572120.png
The link looks like this:
../checkout/order-pay/9762/?pay_for_order=true&key=wc_order_SWO6Y0Nj66YNA

There are a lot of solutions on the Internet, how to redirect to an arbitrary page, one of the proven and working solutions

add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
    function thankyou_custom_payment_redirect(){
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

        // Get the order ID
        $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Set HERE your Payment Gateway ID
        if( $order->get_payment_method() == 'cheque' ){

            // Set HERE your custom URL path
            wp_redirect( home_url( '/custom-page/' ) );
            exit(); // always exit
        }
    }
}


But there is no solution how to get around the useless action when trying to make a payment from the checkout page or from the page of my account, where a similar action is performed.
Despite the fact that during the search for an answer I came across 2 sites where this has already been implemented, I suspect that there is a redirect through js, because the order-pay page opens for a couple of seconds and only then redirects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zeaovede, 2022-04-11
@zeaovede

Googled something and it even works

setTimeout(function(){
  $('button').trigger('click');
}, 1000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question