WooCommerce: Split Cart Into Packages

There are a million plugins out there that allow you to make the most of WooCommerce “cart packages” – this is a short way to say that you have the chance to assign cart items to multiple “packages“, so that the customer can pick different shipping methods for each package.

For example, imagine you sell products that are only available for “pick up in store“, and others that are shippable. By splitting the cart into 2 packages, the customer can place both product types in the same cart, but will be able to choose “Local pickup” for package 1 only, while for package 2 they’ll select one of the available delivery rates.

Splitting the cart into multiple packages is as easy as looping through the cart items, and assigning them to its own package array based on shipping class. Enjoy!

1. Assign a Shipping Class to Each Product

Of course we need to find “something” in order to group products together. For that, we’ll use shipping classes. First of all, create some classes here:

Defining a new shipping class in the WooCommerce settings

Then, assign the shipping class to each product you want to be in the same package:

Assigning a shipping class to a WooCommerce product

2: PHP Snippet: Place Items Into Different Packages Based on Shipping Class @ WooCommerce Cart

/**
 * @snippet       Split WooCommerce Cart Into Multiple Packages
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_cart_shipping_packages', 'bbloomer_split_shipping_packages_by_class' );

function bbloomer_split_shipping_packages_by_class( $packages ) {
		
	$destination = $packages[0]['destination'];	
	$user = $packages[0]['user'];	
	$applied_coupons = $packages[0]['applied_coupons'];
	$packages = array();
	
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {		
		$key = $cart_item['data']->get_shipping_class_id();
		$packages[$key]['contents'][$cart_item_key] = $cart_item;
	}
	
	foreach ( $packages as $index => $package ) {
		$total = array_sum( wp_list_pluck( $packages[$index]['contents'], 'line_total' ) );
		$packages[$index]['destination'] = $destination;
		$packages[$index]['user'] = $user;
		$packages[$index]['applied_coupons'] = $applied_coupons;
		$packages[$index]['contents_cost'] = $total;
	}

	return $packages;

}

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: “You Only Need $$$ to Get Free Shipping!” @ 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. Here’s how we show a simple message on the […]
  • 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

2 thoughts on “WooCommerce: Split Cart Into Packages

  1. When using this with either flat rate or table rate shipping. It uses the combined weight of all cart packages and applies the shipping rate to all packages. Is there a way to get it to apply the relevant rate to each package based on the weight of that package?

    1. That’s weird, because the ‘contents’ array only contains the products of a given package. Could it be your shipping calculator is not compatible with packages?

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 *