Redirect to a Custom Thank-You Page
Sends customers to your own thank-you page after a successful order instead of the default order received page, passing the order number along.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
$page_slug = 'thank-you'; // Slug of your thank-you page.
if ( ! function_exists( 'is_wc_endpoint_url' ) || ! is_wc_endpoint_url( 'order-received' ) ) {
return;
}
$order_id = absint( get_query_var( 'order-received' ) );
$key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
$order = $order_id ? wc_get_order( $order_id ) : false;
$page = get_page_by_path( $page_slug );
if ( ! $order || ! $page || ! hash_equals( $order->get_order_key(), $key ) || $order->has_status( 'failed' ) ) {
return;
}
wp_safe_redirect( add_query_arg( 'order', $order->get_order_number(), get_permalink( $page ) ) );
exit;
} );
Notes
Create the page and set $page_slug. Failed orders are left on the default page so the customer can retry payment.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.