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: Remaining $$$ to Get Free Shipping Notification @ 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. So, here’s how we show a simple message on […]
  • 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

11 thoughts on “WooCommerce: Split Cart Into Packages

  1. Hi Rodolfo,
    Is there a way for the shipping packages to also be displayed in the thank you page and customer order emails?
    The snippet works perfectly in the cart and checkout, but in the thank you page and order emails I’m seeing a combined total of the various shipping options followed by the shipping names in a row seperated by a comma.

    So for example, if I have 5 products each with a different shipping class with shipping costing $20 each, the thank you page and order email would show shipping as:

    $100.00 via Courier, Courier, Courier, Courier, Courier

    Any suggestions would be very much appreciated!

    1. Thanks for your comment! I guess you could look into the “woocommerce_order_shipping_to_display” filter, which allows you to customize the output of the “Order Totals Shipping Row”. With that, you can definitely print each package on its own line, or even on its own table row. Hope this helps!

  2. I applied your snippet but instead of showing as “Shipping” and “Shipping 2”, it is displaying as “Shipping 89″ and Shipping 143”. How do I change this? And how do I stop non-available shipping methods from showing in each?

    1. Very good questions!

      There is a filter to override the “package name” – those numbers you see are the shipping class IDs:

      apply_filters( 'woocommerce_shipping_package_name', ( ( $i + 1 ) > 1 ) ? sprintf( _x( 'Shipping %d', 'shipping packages', 'woocommerce' ), ( $i + 1 ) ) : _x( 'Shipping', 'shipping packages', 'woocommerce' ), $i, $package )
      

      In regard to toggling shipping methods based on the package, the following filter, which is used to do conditional shipping work, also accepts the $package parameter:

      apply_filters( 'woocommerce_package_rates', $package['rates'], $package );
      

      …which means you can target the rates inside a specific package, and hide them when necessary.

      1. Hi Rodolfo,
        Where do I have to insert the 2 lines of code apply_filters please ?
        I’ve put them into the functions.php of my child theme without success.

        1. Hi Patrick, these are the apply_filters “you can hook your custom function to”. So you need to write a full add_filter trigger function and return the edited data.

  3. you were right. i was using “Table Rate Shipping for WooCommerce” plug in and changed to “Fish and Ships” and that works perfectly with the relevant shipping price per Package ๐Ÿ™‚

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

      1. good point. i am using a table rate shipping plugin. I might have to try a different one. I have since found It initially shows the correct shipping per package on the cart page, but then changes to the same price per package at checkout, and if the more expensive one is removed then it doesnt revert back to the lower shipping price. thanks for the suggestion.

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 *