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
}
});
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With