Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce action hook to redirect to custom thank you page

I want to redirect to a custom page after my customers make a payment. Now it goes to a very vanilla, "Your order has been received" page. I have been trying to figure this out for a bit and I'm pretty sure I have to add an action hook to my themes function file. And I found some code that I thought would work but it doesn't.

add_action( 'woocommerce_thankyou', function(){



global $woocommerce;
$order = new WC_Order();
if ( $order->status != 'failed' ) {
wp_redirect( home_url() ); exit; // or whatever url you want
}
});
like image 344
user3905752 Avatar asked Aug 04 '14 07:08

user3905752


People also ask

How do I redirect a thank you page in WooCommerce?

From the WordPress Dashboard go to WooCommerce > Settings > Thank you. To set a redirect after checkout for all products the following options are available: None – when this is selected customers will not be redirected after checkout.

How do I access my WooCommerce thank you page?

The URL for your thank you page can be found by logging into your WordPress admin panel and going to WooCommerce > Settings > Advanced > Thank You Page. If you have not yet set up a thank you page, you can do so by clicking on the Add New Page button.

How do I redirect a page in WooCommerce?

In the callback function, use the wp_redirect() function to add the page where you want the user to be redirected after successful checkout. Always add an exit after the wp_redirect function to avoid redirect problems. This code is added to your functions.


1 Answers

JavaScript redirect? Seriously?

You can use template_redirect with no problems.

Example:

add_action( 'template_redirect', 'correct_redirect' );

function correct_redirect(){

    /* we need only thank you page */
    if( is_wc_endpoint_url( 'order-received' ) && isset( $_GET['key'] ) ) {
        wp_redirect('any url');
        exit;
    }
}

You can find more examples with redirects here https://rudrastyh.com/woocommerce/thank-you-page.html#redirects

like image 82
Misha Rudrastyh Avatar answered Sep 30 '22 18:09

Misha Rudrastyh