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:
Then, assign the shipping class to each product you want to be in the same package:
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;
}
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!
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!
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?
Very good questions!
There is a filter to override the “package name” – those numbers you see are the shipping class IDs:
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:
…which means you can target the rates inside a specific package, and hide them when necessary.
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.
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.
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 ๐
Awesome
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?
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?
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.