In my WooCommerce store I would like to redirect to a custom 'thank you page' after payment has been completed. I'm using this code:
add_action( 'template_redirect', 'myfunction' );
function myfunction( $order_id ) {
$order = wc_get_order( $order_id ); // I assume this is where I go wrong. Probably this is empty?
if ( is_checkout() && 0 == WC()->cart->get_cart_contents_count() ) {
wp_safe_redirect( home_url() ); // redirect to home if empty cart on checkout page
exit;
} else if ( is_checkout() && **somehow check if $order has been paid?** ) {
wp_safe_redirect( 'www.myurl.com' ); // if order has been paid for go here. this never triggers.
exit;
}
}
I assume that my problem is that the $order_id is empty, but my debugging skills suck so I don't know how to actually verify that to even get started.
I know of the $order->get_status(). I suppose this is called a method? If I try that in the place of ** ** I get a php fatal error.
Thanks for any help.
woocommerce_payment_successful_result
to customise the redirection URL : docs.woocommerce.com/wc-apidocs/…