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, Business Bloomer
 * @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

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

15 thoughts on “WooCommerce: Split Cart Into Packages

  1. I have used this code for split shipping packages. After that, I faced an issue with doubling the shipping cost. Can you please help resolve this issue.

    1. If you split the cart into packages, each package will be priced accordingly to the shipping rate you select, so this will be based on your shipping rate setup

      1. Yes, I have split the cart items according to product shipping class. I have two shipping classes when I add both shipping class products the shipping cost is double. I have set a flat shipping rate. I have two shipping methods. like class A and B method A=0 and B=10 or A=12 and B=0,

        1. This may require proper troubleshooting, sorry but I can’t help you here via the comments. I’m 100% sure that this can be done/fixed.

  2. 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!

  3. 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.

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

  5. 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 *