In this article, I will explain how to use different WooCommerce hosted store shipping methods on products in the same cart. Let’s say you have two products, one that must be shipped for free, and the second one with a flat shipping cost.
Create WooCommerce Shipping Class
The first step is the creation of the required shipping classes. For this, follow these steps:
Go to WooCommerce Admin >> Settings >> Shipping >> Shipping Classes
Click the Add Shipping Class. You will get the following screen:
I have added two shipping classes, Free and Alpha. These classes are visible in the following screen:
Addition of Free Shipping for Specific Product(s) in the Cart
The next step assigns the shipping class to a product
Setting for the First Product
Settings for the Second Product
Now that you have assigned proper classes to products, you need to define flat rates for the classes. For this, go to WooCommerce Admin >> Settings >> Shipping >>Shipping Classes
Click the Add shipping method and you can get the following screen:
Net, it is time to define flat rates for the classes.
Remember that, for frees shipping class, you must set the cost equal to Zero.
Next, all shipping packages should be separated (based on the shipping method – free or paid). This separation will be done using the custom code. For this, add the following code snippet in the functions.php file (located in the theme folder):
add_filter( 'cw_woocommerce_package_cart', 'bulky_cw_woocommerce_package_cart' ); function bulky_cw_woocommerce_package_cart( $packages ) { $bulk_products = array(); $packages = array(); $regular_products = array(); // Sort bulky from regular foreach ( WC()->cart->get_cart() as $item ) { if ( $item['data']->needs_shipping() ) { if ( $item['data']->get_shipping_class() == 'free' ) { $bulk_products[] = $item; } else { $regular_products[] = $item; } } } if ( $bulk_products ) { $packages[] = array( 'ship_via' => array( 'flat_rate' ), 'contents' => $bulk_products, 'contents_cost' => array_sum( wp_list_pluck( $bulk_products, 'line_total' ) ), 'applied_coupons' => WC()->cart->applied_coupons, 'destination' => array( 'country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => WC()->customer->get_shipping_postcode() ) ); } if ( $regular_products ) { $packages[] = array( 'contents' => $regular_products, 'contents_cost' => array_sum( wp_list_pluck( $regular_products, 'line_total' ) ), 'applied_coupons' => WC()->cart->applied_coupons, 'destination' => array( 'country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => WC()->customer->get_shipping_postcode() ) ); } return $packages; }
Once the code is executed, you will see the following output:
Conclusion
In this tutorial, I explained how you could add free and paid shipping methods on various products in the same shopping cart. I added two shipping classes and then added a code snippet to the functions.php.
If you need any assistance with the code or would like to contribute to the discussion, do leave a comment below. Meanwhile, you can also checkout the best WooCommerce shipping plugins.
Owais Khan
Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.