Home » WooCommerce: Split Cart Items When Product Quantity > 1

WooCommerce: Split Cart Items When Product Quantity > 1

by Tutor Aspire

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 tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=72541
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.5.1
 * @donate $9     https://tutoraspire.com
 */

// -------------------
// 1. Split product quantities into multiple cart items
// Note: this is not retroactive - empty cart before testing

function tutoraspire_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', 'tutoraspire_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 may also like