WooCommerce Redirect Snippet Generator

Send customers to a custom page after checkout, login, registration or add to cart, by product or user role.

Describe the result you need. AI will set up the tool and ask for any missing details.

Configure with AI

Enter to submit · Shift + Enter for a new line

Sent to AI. Leave out passwords and private customer data.

Your details

Redirect

A path on your site like /thank-you/, or a full URL.

Only for

So a custom page can look up and show the order.

Code

Keeps function names unique.

Paste into your child theme's functions.php or a code snippets plugin

redirect-after-checkout.php
/**
 * WooCommerce redirect. Paste into your child theme's functions.php or a code snippets plugin.
 */
add_action( 'template_redirect', 'my_store_redirect_after_checkout' );
function my_store_redirect_after_checkout() {
	if ( ! function_exists( 'is_wc_endpoint_url' ) || ! is_wc_endpoint_url( 'order-received' ) ) {
		return;
	}
	global $wp;
	$order_id = isset( $wp->query_vars['order-received'] ) ? absint( $wp->query_vars['order-received'] ) : 0;
	$order    = $order_id ? wc_get_order( $order_id ) : false;
	$key      = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

	// Only real orders that belong to this visitor, and never failed payments (they need the retry button).
	if ( ! $order || ! $order->key_is_valid( $key ) || $order->has_status( 'failed' ) ) {
		return;
	}

	$url = home_url( '/thank-you/' );
	wp_safe_redirect( $url );
	exit;
}

The standard thank-you page never renders, so anything that fires there (the woocommerce_thankyou hook, some ad pixels and analytics purchase events) won't run. Check your tracking before going live.

Works with the block and classic checkout, because both finish on the order-received page. It uses the order object, so it's HPOS-compatible.

Which hook each redirect uses

Redirect afterHookNotes
Checkouttemplate_redirect on the order-received pageChecks the order key and skips failed payments. Works with block and classic checkout.
Loginwoocommerce_login_redirectMy account login form. Can check the user's role.
Registrationwoocommerce_registration_redirectMy account registration form.
Add to cartwoocommerce_add_to_cart_redirectPage-reload adds only, not AJAX buttons.

Order checks use wc_get_order() and the order object's methods, so the snippet is compatible with High-Performance Order Storage (HPOS).

Installing it

  1. 01

    Create the destination page

    In WordPress, add the page you're redirecting to (for example a thank-you page with next steps) and publish it.

  2. 02

    Paste the snippet

    Add it to your child theme's functions.php or a code snippets plugin. Don't use both a snippet and a redirect plugin for the same redirect.

  3. 03

    Test each case

    Place a test order (or log in, or add to cart) as a customer, including one that shouldn't match your condition.

If you pass the order number and key to your page, you can show order details there with wc_get_order(), checking the key first.

Pitfalls

  • Tracking on the thank-you page stops firing. Anything hooked to woocommerce_thankyou, including many ad pixels, won't run. Move tracking to your new page or a server-side integration.
  • Other domains are blocked by WordPress's safe redirect unless allowed. The tool adds the allowed_redirect_hosts filter when you enter a full URL.
  • AJAX add to cart skips the filter. Untick Enable AJAX add to cart buttons on archives under WooCommerce → Settings → Products if you need shop-page buttons to redirect.

Questions people ask

How do I redirect to a custom thank-you page in WooCommerce?

Pick Checkout, enter your page's path and paste the snippet. It redirects from the order-received page once the order key checks out.

Can I redirect only for certain products?

Yes. Choose “Only for certain products” and list product or variation IDs. The order is checked for any of them.

Will failed payments be redirected?

No. Failed orders stay on the standard page so customers can retry payment.

Does the login redirect work on wp-login.php?

No. It covers WooCommerce's My account login form. The WordPress login screen uses the core login_redirect filter.

Want this builtinto your store?

I'm Emmanuel, a Shopify and WooCommerce developer. If this tool pointed at something you'd rather have fixed for you (a faster store, cleaner product data, a migration, a custom feature), send me a note.

Emmanuel Asika

Emmanuel Asika

Shopify & WooCommerce developer, Dublin

I build and fix online stores: new builds, migrations, speed, SEO and the custom bits apps can't do.

hello@emmanuelasika.com