How to display category specific message on woocommerce’s thank you page?

In this tutorial we’ll learn how to display category specific message on thank you page of woocommerce.

To do this add the following lines of code at the end of your theme’s functions.php file:

function so_28348735_category_based_thank_you_message ( $order_id ){

   $order = wc_get_order( $order_id );

   $show = false;

foreach( $order->get_items() as $item ) {

       // check if a product is in specific category

if ( has_term( 'category-1', 'product_cat', $item['product_id'] ) ) {

           $show = true;

continue;

       }

   }

if( $show ){

echo 'Thanks for purchasing product from category 1';

   }

}

add_action( 'woocommerce_thankyou', 'so_28348735_category_based_thank_you_message' );

 

You can replace your category slug with the slug I’ve written i.e ‘category-1’, also you can replace the thank you message with the one you want to display.

Now when I add a product from category 1 and proceed to checkout and payment steps I’ll find the custom message being displayed.

First of all add product to cart.

woocommerce

Now move to cart.

woocommerce

Now proceed to checkout page from here.

woocommerce

After providing billing, shipping details and selecting payment method place your order.

After placing the order you’ll see the custom message being displayed.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *