add_action('template_redirect', 'custom_wc_thankyou_redirect'); function custom_wc_thankyou_redirect() { if ( is_wc_endpoint_url('order-received') && ! empty($_GET['key']) ) { $order_id = wc_get_order_id_by_order_key($_GET['key']); if ($order_id && 'failed' !== wc_get_order($order_id)->get_status()) { wp_safe_redirect(site_url('/thankyou')); exit; } } }