WooCommerce: Split Cart Items When Product Quantity > 1
Quite an interesting snippet this is! A client needed to show EACH quantity of the SAME product as separate lines (cart items) in the WooCommerce Cart & Checkout page.
At some stage, you might need that too – for different reasons maybe. So, let’s see how this is coded π
WooCommerce: split product into multiple cart items
PHP Snippet: Display Separate Cart Items for Product Quantity > 1
/**
* @snippet Display Separate Cart Items for Product Quantity > 1 | WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=72541
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.5.1
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
// -------------------
// 1. Split product quantities into multiple cart items
// Note: this is not retroactive - empty cart before testing
function bbloomer_split_product_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = uniqid();
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_split_product_individual_cart_items', 10, 2 );
// -------------------
// 2. Force add to cart quantity to 1 and disable +- quantity input
// Note: product can still be added multiple times to cart
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
Does this snippet (still) work?
Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.
If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
Hello Tak, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
I put this snippet of code in the ‘snippets’ module it didn’t work. Then I added functions.php from the child theme file, but it still doesn’t work. I mention that, woocommerce version V5.7.1 with wordpress 5.8.2.
The error message is standard: another ‘Product Name’ cannot be added to your cart.
Thanks so much for you help, it worked great. I needed it for a webhook I was running on purchase that loops through the line_items and sens a request to thirdparty api so this saves me from having to check for quantity in the for loop.
Never comment, always the lurker.
This snippet though solved my problem.
My usecase is a products which may or may not have custom user input as part of the product. Think a poster with text.
Each product is unique due to the user input provided but is considered the same product in woo.
Many thanks for putting this online.
I will poor a beer out in your honor!
Great coding as always, thank you! I have used this code in part but left out the last line: add_filter( ‘woocommerce_is_sold_individually’, ‘__return_true’ ); as I want to add the same product individually but need to keep the selected quantities. This works fine, however we use a plugin for advanced dynamic pricing which discounts and shows the total of the individual item in the cart. This still shows on the first addition but not the second. Can you help please?
Hi Brian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I wonder if it is possible to have a product in the cart and if is add a second product it has the same quantity that the first product added in the cart?
Hi Dylan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
But if i’m on a PDP, and add the product with qty 2 towards the shoppingcart, the product qty is grouped instead of split into seperate products.
when i in- decrease the qty in the cart it self, then the products will spil up (maybe because i’ve auto update prices activated)
or does this issue cause because i’ve disabled scripts to increase woocommerce speed…
Although your solution is great, and i have been using until today, i just faced a big problem.
Analytics cant find more than 1 product in the cart (for the total price). This is a problem, as now im starting to analyze my shopping results, and not being able to track how many products has been sold on average is a problem.
Do you have any idea on how can I fix this, and of course improve your snippet?
Hi Andres, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey Rodolfo, learned a lot from you and this website, thanks about that first.
I am using your solution to develop the mini cart ajax functionality. But it appears that sometimes when I am deleting products or adding one, product is overriten by the newly added.
Have you experienced that issue beffore ?
I try also to pump up the random id with microtime() . rand() . uniqid() and then return it.
Hello Rudolf, I’m going to need your help,
My case is as follows:
The customer can add a product because it may be needed when needed on the cart page.
But if you add it again the products of the product page will have to stay separate.
This is because the product is customizable,
product it can upload a file.
Then the customer can order 30 items with the same customization. Then you can go back to the products and add the same item, send another upload file.
It will have the same album the same item … only with two types of customization.
You can make this change
When the customer is going to add it again the product has just added to the product already added.
Hey Diuliano, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
I’ve been looking for at code to do exactly this, unfortunately I cannot make it work when I add it to my functions.php. Any suggestions on what I’m doing wrong here?
Thanks for your reply. Tried to empty cart and tried from another computer (that has not visited the shop previously) and it still does not work. I can’t add another of the same item, when it’s already in the cart.
I think this snippet is great except for the fact that it completely removes the ability to adjust/readjust the QTY before or after the items are in the cart. Is it possible to make an adjustment to the code?
Josh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi! First Rodolfo thanks for the snippet, works like a charm on woocommerce 3. I put it in a plugin and it works as expected.
@Josh: If you remove the code following comment “// ——————-
// 2. Force add to…” you can achieve what you are looking for. You can still change the quantity on product page (not in the cart though).
Hello Pat, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello Rodolfo,
Thank you very much for all the code snippets you develop… you are awesome!
I would like to know if there is a way to separate the cart items by category. See I have a website in which I’m using Woocommerce to sell food products from different brands. These brands are configured to be the main category. Their food type is set as subcategory and finally the product is the food itself. Ex.:
Dino’s Pizza > Gourmet Pizza > Canadian.
Texas Burger > Hamburgers > Texas JalapeΓ±o Burger.
I need the cart items to be separated by categories, one category per page. I believe the same code will also allow me to separate the items in the pdf invoice and order emails too so when an order is received I can go and show the respective order to the Brand without them seeing what the client is purchasing from other brands π .
Hola JC π Thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Is this possible for only the checkout page?
For example,
Add to cart
Product A x 3
Product B x5
Cart Page
Product A x 3
Product B x 5
Checkout Page
Product A x1
Product A x1
Product A x1
Product B x 1
Product B x 1
Product B x 1
Product B x 1
Product B x 1
Best Regards
“Is it possible” as in with tweaking the code?
I have tried several ways but could not get it to work.
Hello Tak, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
I put this snippet of code in the ‘snippets’ module it didn’t work. Then I added functions.php from the child theme file, but it still doesn’t work. I mention that, woocommerce version V5.7.1 with wordpress 5.8.2.
The error message is standard: another ‘Product Name’ cannot be added to your cart.
Thank you!
Weird, works for me. Maybe it’s a setting or a plugin conflict. Try again with no other plugin active (but WooCommerce)
Thanks so much for you help, it worked great. I needed it for a webhook I was running on purchase that loops through the line_items and sens a request to thirdparty api so this saves me from having to check for quantity in the for loop.
Awesome
Never comment, always the lurker.
This snippet though solved my problem.
My usecase is a products which may or may not have custom user input as part of the product. Think a poster with text.
Each product is unique due to the user input provided but is considered the same product in woo.
Many thanks for putting this online.
I will poor a beer out in your honor!
Cheers
this is so weird, in my case, my woocommerce did this without any modification, i use elementor with hello theme. any clue ?
No clue
Thanks for this tip.
Welcome!
Great coding as always, thank you! I have used this code in part but left out the last line: add_filter( ‘woocommerce_is_sold_individually’, ‘__return_true’ ); as I want to add the same product individually but need to keep the selected quantities. This works fine, however we use a plugin for advanced dynamic pricing which discounts and shows the total of the individual item in the cart. This still shows on the first addition but not the second. Can you help please?
Hi Brian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi there
I wonder if it is possible to have a product in the cart and if is add a second product it has the same quantity that the first product added in the cart?
Hi Dylan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Question, code works great.
But if i’m on a PDP, and add the product with qty 2 towards the shoppingcart, the product qty is grouped instead of split into seperate products.
when i in- decrease the qty in the cart it self, then the products will spil up (maybe because i’ve auto update prices activated)
or does this issue cause because i’ve disabled scripts to increase woocommerce speed…
Kevin, if you re-enable scripts, does the snippet work? (Also empty your cart before testing again and clear customer sessions)
Hi Rodolfo,
Although your solution is great, and i have been using until today, i just faced a big problem.
Analytics cant find more than 1 product in the cart (for the total price). This is a problem, as now im starting to analyze my shopping results, and not being able to track how many products has been sold on average is a problem.
Do you have any idea on how can I fix this, and of course improve your snippet?
Thanks
Kind regards
Hi Andres, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey Rodolfo, learned a lot from you and this website, thanks about that first.
I am using your solution to develop the mini cart ajax functionality. But it appears that sometimes when I am deleting products or adding one, product is overriten by the newly added.
Have you experienced that issue beffore ?
I try also to pump up the random id with microtime() . rand() . uniqid() and then return it.
Any Ideas?
Thanks,
Cheers
Not 100% sure Petar, if you find a fix let me know!
Hi,
My woocommerce is by default splitting the product quantities in the cart, is there anyway I can unsplit them?
Hey Angad, thanks for your comment! that’s probably an out of date theme or some plugin. Disable all temporarily and see if the problem goes away π
Hello Rudolf, I’m going to need your help,
My case is as follows:
The customer can add a product because it may be needed when needed on the cart page.
But if you add it again the products of the product page will have to stay separate.
This is because the product is customizable,
product it can upload a file.
Then the customer can order 30 items with the same customization. Then you can go back to the products and add the same item, send another upload file.
It will have the same album the same item … only with two types of customization.
You can make this change
When the customer is going to add it again the product has just added to the product already added.
Send me an e-mail.
Hey Diuliano, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi Rodolfo
I’ve been looking for at code to do exactly this, unfortunately I cannot make it work when I add it to my functions.php. Any suggestions on what I’m doing wrong here?
Best regards
Hey Carina, thanks so much for your comment! Try emptying the Cart first, this is not retroactive π
Hi Rodolfo
Thanks for your reply. Tried to empty cart and tried from another computer (that has not visited the shop previously) and it still does not work. I can’t add another of the same item, when it’s already in the cart.
Just an update – made it work for my purpose by deleting “part two” of your code snippet π
Great π
Hi Rodolfo,
I think this snippet is great except for the fact that it completely removes the ability to adjust/readjust the QTY before or after the items are in the cart. Is it possible to make an adjustment to the code?
Thanks,
Josh
Josh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi! First Rodolfo thanks for the snippet, works like a charm on woocommerce 3. I put it in a plugin and it works as expected.
@Josh: If you remove the code following comment “// ——————-
// 2. Force add to…” you can achieve what you are looking for. You can still change the quantity on product page (not in the cart though).
Regards,
Branka
Is it possible to create an order that will keep track of the following per line item
Firstname > Lastname > Product Title
Building a Convention / Seminar Shopping Cart and need to keep track of which customer will be attending which seminar.
Can’t figure out where to store the Customer Name and Seminar the the WooCommerce Database.
How many line items can I add to my cart?
Hello Pat, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello Rodolfo,
Thank you very much for all the code snippets you develop… you are awesome!
I would like to know if there is a way to separate the cart items by category. See I have a website in which I’m using Woocommerce to sell food products from different brands. These brands are configured to be the main category. Their food type is set as subcategory and finally the product is the food itself. Ex.:
Dino’s Pizza > Gourmet Pizza > Canadian.
Texas Burger > Hamburgers > Texas JalapeΓ±o Burger.
I need the cart items to be separated by categories, one category per page. I believe the same code will also allow me to separate the items in the pdf invoice and order emails too so when an order is received I can go and show the respective order to the Brand without them seeing what the client is purchasing from other brands π .
Thank you!
JC
Hola JC π Thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Yes Sir, works perfect!
Thank you!!
Great π