This is similar to a question I posted previously here but the calculation I need for a new project is different.
Rather than showing a notice based on the total quantity of items in the cart, I need to show a notice if any one product in the cart has a quantity of 1. If no items in the cart have a quantity of 1 then it doesn’t need to show, but if any item does then it needs to show. Is this possible?
2
Answers
This is what I used in the end, thanks to mpx9910's answer (thank you!):
This function loops over cart items, if quantity of a cart item is equal to 1 the notice is shown and the function ends.
You can use ‘woocommerce_check_cart_items’ hook, or other cart hooks based on where you need to print the notice.
Here is also a modified answer to exclude products in certain category from triggering the notice (question from comment):
Whenever a product with quantity 1 is found, there is another nested loop that iterates over the product’s categories, if any of the categories matches the one that you don’t want to trigger the notice then notice is not displayed and the outer loop continues.
Don’t forget to replace ‘your-category-slug’ with slug of the category you don’t want to trigger the notice.