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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @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' );

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). 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+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

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

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

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

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

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

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

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

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

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

  9. Yes Sir, works perfect!

    Thank you!!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.