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

Share:

Leave a Reply