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.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How To Add Multiple Shipping Methods in WooCommerce Cart

Updated on June 18, 2021

3 Min Read

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

Share your opinion in the comment section. COMMENT NOW

Share This Article

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 [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

Comments are closed.

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now