How to split woocommerce shipping package based on shipping class?

In WooCommerce version 2.1 a new feature was introduced that allowed one order to have multiple shipping packages. Having multiple shipping packages can come in handy in various situations.

One of my upwork client asked me to allow free shipping on just a specific set of products. When using a shipping carrier plugin fedex. it can be hard to set this up, but when splitting your order into multiple packages, you can target only the package with the free shipping items, while the fedex plugin still gets the shipping cost for the other products.

References

https://jeroensormani.com/splitting-shipping-packages-in-woocommerce/

woocommerce-split-shipping-packages-example

Need Help? Get help at fb.me/getwpsupport

Splitting Shipping Package based on Shipping Class

The following code snippet will split the shipping package based on the shipping class. Each product that has a shipping class A will be split into a separate package from the other products.

<?php
/**
 * Split all shipping class 'A' products in a separate package
 */
function custom_split_shipping_packages_shipping_class( $packages ) {

	// Reset all packages
	$packages              = array();
	$regular_package_items = array();
	$split_package_items   = array();

	$split_shipping_class = 'a'; // Shipping class slug
	foreach ( WC()->cart->get_cart() as $item_key => $item ) {

		if ( $item['data']->needs_shipping() ) {

			if ( $split_shipping_class == $item['data']->get_shipping_class() ) {
				$split_package_items[ $item_key ] = $item;
			} else {
				$regular_package_items[ $item_key ] = $item;
			}

		}

	}

	// Create shipping packages
	if ( $regular_package_items ) {
		$packages[] = array(
			'contents'        => $regular_package_items,
			'contents_cost'   => array_sum( wp_list_pluck( $regular_package_items, 'line_total' ) ),
			'applied_coupons' => WC()->cart->get_applied_coupons(),
			'user'            => array(
				 'ID' => get_current_user_id(),
			),
			'destination'    => array(
				'country'    => WC()->customer->get_shipping_country(),
				'state'      => WC()->customer->get_shipping_state(),
				'postcode'   => WC()->customer->get_shipping_postcode(),
				'city'       => WC()->customer->get_shipping_city(),
				'address'    => WC()->customer->get_shipping_address(),
				'address_2'  => WC()->customer->get_shipping_address_2()
			)
		);
	}

	if ( $split_package_items ) {
		$packages[] = array(
			'contents'        => $split_package_items,
			'contents_cost'   => array_sum( wp_list_pluck( $split_package_items, 'line_total' ) ),
			'applied_coupons' => WC()->cart->get_applied_coupons(),
			'user'            => array(
				 'ID' => get_current_user_id(),
			),
			'destination'    => array(
				'country'    => WC()->customer->get_shipping_country(),
				'state'      => WC()->customer->get_shipping_state(),
				'postcode'   => WC()->customer->get_shipping_postcode(),
				'city'       => WC()->customer->get_shipping_city(),
				'address'    => WC()->customer->get_shipping_address(),
				'address_2'  => WC()->customer->get_shipping_address_2()
			)
		);
	}

	return $packages;

}
add_filter( 'woocommerce_cart_shipping_packages', 'custom_split_shipping_packages_shipping_class' );

Leave a Reply

Your email address will not be published. Required fields are marked *