How to Create WooCommerce Redirect After Checkout Per Product

WooCommerce redirect after checkout per product

When you are selling products that require specific after-sale service on your WooCommerce store, you may want to create WooCommerce redirect after checkout per product.

Ideally, for every product, the user will be redirected to a specific page.

In the previous post, I shared in detail how to create WooCommerce redirect after checkout, and in another post how to create a WooCommerce redirect after logout.

WooCommerce Redirect After Checkout Per Product

In this post, I am going to show you how to quickly create a custom WooCommerce redirect after checkout for each product that is bought individually.

Redirect After Checkout Per Product Code Snippet

To create the WooCommerce Redirect After Checkout Per Product using a code snippet, you need to add the code snippet to your functions.php file and you also need to understand how the code works.

The idea is to get the product in the cart and use the ID to redirect the user after checkout if the product is in the cart. You can implement this in the code as shared in the code snippet below :


add_action( 'woocommerce_thankyou', 'njengah_redirect_after_checkout_per_product', 1 );

function njengah_redirect_after_checkout_per_product ( $order_id ){
$order = wc_get_order( $order_id );

foreach( $order->get_items() as $item ) {
// Change the product_id to match the product_id of your choice
if ( $item['product_id'] == 1 ) {
// Redirect URL
wp_redirect( 'example.com' );
} else {
// Alternative Redirect UR
wp_redirect( 'example.com' );
}
}
}

You need to edit the code above with the right product ID and the redirect URL for it to work.

Redirect After Checkout Per Product Plugin

If you require a quick plugin solution, I created a plugin WRAC that comes with excellent conditional redirects that allow users to create WooCommerce redirects after checkout for each product as you can see in the image below.

WooCommerce redirect after checkout per product plugin

Conclusion

In this post, we have covered the way you can create WooCommerce Redirect After Checkout Per Product both using a plugin and also using a code snippet.

If you would like to implement this solution and you are not sure how to get it done, you can feel free to get in touch.

Similar Articles

  1. How to Clear Cart on Logout In WooCommerce
  2. How to Hide Price and Add to Cart for Logged Out Users WooCommerce