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
 * @community     https://businessbloomer.com/club/
 */

// -------------------
// 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' );

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: Remaining $$$ to Get Free Shipping Notification @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. So, here’s how we show a simple message on […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]

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. Follow @rmelogli

43 thoughts on “WooCommerce: Split Cart Items When Product Quantity > 1

  1. I am trying to replicate this example, because I am interested in applying this to the varieties of a product.

    My scenario is as follows:

    I am adding the product to the cart using a custom function with ajax, to which I pass the variety ID and the custom metas I want to add to the order.

    but I have not been able to make these variable products to be added as separate products to the cart, basically this is the code I use to add the variable product to the cart:

    $unique_key = md5(microtime().rand()); 
    
    $cart_item_data = array(
    				'key' => $unique_key ,
    				'product_id' => $product_id,
    				'quantity' => $quantity,
    		
    			);
    
     WC()->cart->add_to_cart($product_id, $quantity,  $variation_id, array(), $cart_item_data);
    
    $cart_item  = WC()->cart->get_cart_item($cart_item_key);
    $cart_item['custom_meta_1'] 	=  $custom_meta_1;
     $cart_item['custom_meta_2'] 	= $custom_meta_1;
    
    WC()->cart->cart_contents[$cart_item_key] = $cart_item;
     WC()->cart->set_cart_contents(WC()->cart->cart_contents);
     WC()->cart->calculate_totals();
    
    wp_send_json_success(WC()->cart->get_cart()[$cart_item_key]);
    
    1. Hello Ysidro, 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!

  2. 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

    1. “Is it possible” as in with tweaking the code?
      I have tried several ways but could not get it to work.

      1. 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!

  3. 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!

    1. Weird, works for me. Maybe it’s a setting or a plugin conflict. Try again with no other plugin active (but WooCommerce)

  4. 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.

    1. Awesome

  5. 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!

    1. Cheers

  6. this is so weird, in my case, my woocommerce did this without any modification, i use elementor with hello theme. any clue ?

    1. No clue

  7. Thanks for this tip.

    1. Welcome!

  8. 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?

    1. 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!

  9. 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?

    1. 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!

  10. 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…

    1. Kevin, if you re-enable scripts, does the snippet work? (Also empty your cart before testing again and clear customer sessions)

  11. 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

    1. 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!

  12. 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

    1. Not 100% sure Petar, if you find a fix let me know!

  13. Hi,

    My woocommerce is by default splitting the product quantities in the cart, is there anyway I can unsplit them?

    1. 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 πŸ™‚

  14. 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.

    1. 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

  15. 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

    1. Hey Carina, thanks so much for your comment! Try emptying the Cart first, this is not retroactive πŸ™‚

    2. 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.

    3. Just an update – made it work for my purpose by deleting “part two” of your code snippet πŸ™‚

  16. 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

    1. 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

    2. 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

  17. 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?

    1. 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

  18. 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

    1. 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

  19. Yes Sir, works perfect!

    Thank you!!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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