Reputation: 73
I want to redirect to a custom wordpress page if a customers woocommerce order fails.
I have found and implemented the following code which redirects to a new page upon payment success.
Is it possible to add to this code, so a failed order is sent to another specific url ?
add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom');
function bbloomer_redirectcustom( $order_id ){
$order = wc_get_order( $order_id );
$url = 'https://yoursite.com/custom-url';
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url );
exit;
}
}
Upvotes: 1
Views: 2349
Reputation: 29614
Based on the code you have, you can use $order->has_status( 'failed' )
So you get:
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Settings
$url1 = 'https://yoursite.com/custom-url-1';
$url2 = 'https://yoursite.com/custom-url-2';
// Compare
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url1 );
exit;
} else {
wp_safe_redirect( $url2 );
exit;
}
}
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
Code goes in functions.php
file of the active child theme (or active theme). Tested and works in Wordpress 5.8.1 & WooCommerce 5.8.0
Upvotes: 1