How to modify product name on the basis of quantity on order page in Woocommerce?

We often come up with a need to modify product title a bit in orders on the basis of quantity. For example a customer purchase a single unit of product i.e. candle if the customer purchase six of it let’s say then we want to display pack of candle instead of just candle.

Yes, we can do it with just a few lines of code instead of messing up with product titles. Let’s do it. We’ll display every product with quantity greater than 4 as pack of product.

To view an order go to My Account page and click on view button against an order to view its details.

woocommerceIf you don’t have any order or don’t have order of quantity 5 or more, then I would suggest you would create one.

To create an order add a product to cart, go to cart page set product quantity to at least 5. Now proceed o checkout and provide details to place order.

Here is the default order detail page:

woocommerceNow to add a string based on quantity add following lines of code at the end of your theme’s functions.php file:

add_filter( 'woocommerce_order_item_name', 'so_29985124_order_item_title', 10, 2 );

function so_29985124_order_item_title( $title, $order_item ) {

 

   if ( $order_item[ 'qty' ] > 4 ) {

       $title = "Pack of " . $title;

   }

 

   return $title;

}

You can change the quantity and text to anything of your choice.

You can access functions.php file here:

woocommercePlace the code and click on update file button to save it.

Now, if you go to order detail page again you’ll see product title being changes as modified only for the products having quantity greater than 4.

woocommerce

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 *