Although this is a rarely used codex to Treat cart items separately if the quantity is more than 1 but we still want to share this tinny snippet which can be helpful to someone like me. In my live woocommerce project, there was a requirement from the client that if there is a woocommerc order having a product with more than one quantity then the cart item should be treated as a subitem for the quantity of the item in the cart.
Add the following code to your active theme’s functions.php
1 2 3 4 5 6 7 8 9 10 |
add_action( 'woocommerce_add_to_cart', 'DCS_splitCartItems', 10, 6 ); function DCS_splitCartItems( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { if ( $quantity > 1 ) { WC()->cart->set_quantity( $cart_item_key, 1 ); for ( $i = 1; $i <= $quantity -1; $i++ ) { $cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" ); WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data ); } } } |
This is all. Now your cart will start showing cart items separate if quantity is more than 1
Happy coding !!!! 🙂