I’m trying to work on something new for displaying 2 different thank you pages for the woocommerce plugin. When a user has selected a different shipping method like local pickups I want to redirect them to different thank you pages after checkout.
I have added the following code but it doesn’t works redirects to the default. What am I doing wrong
// Thank you page redirect
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-1/' );
exit;
}
elseif( $order->has_shipping_method('local_pickup:7') ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-2/' );
exit;
}
elseif( $order->has_shipping_method('flat_rate:1') ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-3/' );
exit;
}
}
Not sure what’s happening, might be I’m missing or doing something wrong.
3
Answers
Does it have to be a redirect? I don’t know exactly what you’re up to, but maybe it is enough to display different content, depending on the shippment method? If so, you can edit the thankyou.php template.
First copy the file
plugins/woocommerce/templates/checkout/thankyou.php
to your theme directorythemes/(your-theme)/woocommerce/checkout/tankyou.php
. Here you can change the thankyou template, without losing the changes after a woocommerce update. (Template structure & Overriding templates via a theme)Now you can adjust the
thankyou.php
(inside your theme directory) and add your conditions at the right place://Use this hook to target your thankyou page.
In your function, you don’t define
$order
. You need to get the $order Object before you can access any of the methods. Since$order
is not defined, the other caseselseif
are never true.