Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Add Multiple WooCommerce Shipping Methods On Cart Pages

March 28, 2017

3 Min Read
Reading Time: 3 minutes

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.

free shipping per product basis

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 have any question about the topic of this tutorial, just leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Your WooCommerce Store Needs A Reliable Host

With no compromise on Performance, Security & Support.

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!