2

We need to run WooCommerce flat rate shipping rate based on every X items in the cart, for example:

  • 1-5 Items: Delivery = £5
  • 6-10 Items: Delivery = £10
  • 11-15 Items: Delivery = £15
  • and so on…

It's essentially £1 per item, but it's in brackets of £5, so 3 items would be £5, and 7 items would be £10, for example.

I'm hoping this is achievable with the out of the box shipping rates, or alternatively, I'd be relatively comfortable writing a theme function, if required - but the client has a limited budget on this.

I've tried looking for more reference on the available shortcodes that I can use in the advanced shipping options, but can't seem to find a way to achieve the above.

3

For that, you need to use a hooked function in woocommerce_package_rates filter hook, targeting "Flat rate" shipping method.

In shipping settings for flat rate shipping method you will set a cost of 5.

Each 5 items step, the shipping cost will be increased by 5:
• From 1 to 5 Items: Delivery = 5
• From 6 to 10 Items: Delivery = 10
• From 11 to 15 Items: Delivery = 15
And so on…

Here is the code:

add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_steps', 10, 2 );
function different_rates_based_on_quantity_steps( $rates, $package ){

    $items_count  = WC()->cart->get_cart_contents_count(); // Cart item count
    $items_change = 5; // number of items needed to increase the cost each time
    $rate_operand = ceil( $items_count / $items_change ); // Operand increase each 5 items here

    foreach ( $rates as $rate_key => $rate ){
        // Targetting "Flat rate"
        if( 'flat_rate' === $rate->method_id ) {
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $rate_operand;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $rate_operand;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Refresh the shipping caches: (required)

  1. This code is already saved on your active theme's function.php file.
  2. The cart is empty
  3. In a shipping zone settings, disable / save any shipping method, then enable back / save.
  • Dude! You're a life saver! I owe you a beer! Anyhow, I need this tweaked. Instead doubling the shipping by the total of items in the cart. Can this be done by the number of items in a specific shipping class? In other words, Say I have 2 types of products with a shipping class each (Large and Small). Small is something that can be included in the large package or sold by itself. I don't need the shipping cost to double just cause I added more than 5 of those since they don't affect the size of the enitre package. Only the Large class affects the size after 5 of them. – LOTUSMS Apr 24 '19 at 14:31
  • How can change this $items_count = WC()->cart->get_cart_contents_count() to get the count of a shipping class alreaady established or slugged (large-shipping). Something like $items_count = WC()->cart->get_shipping_class('large-shipping'), Which I obviously know it doesn't exist lol – LOTUSMS Apr 24 '19 at 14:37
0

To add a specific class you must check each item in the cart, and check with the slug of the class which items correspond, in this way:

First add the woocommerce_package_rates filter

add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_steps', 10, 2 );

Then insert the function

function different_rates_based_on_quantity_steps ( $rates, $package ){

$slug_class_shipping = 'books'; // Slug of the shipping class
$step_change = 5; // Steps for item
$item_count = 0;

// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){

// If we find the shipping class
if( $cart_item['data']->get_shipping_class() == $slug_class_shipping ){

$item_count += $cart_item['quantity']; // Cart item count
$rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here

foreach ( $rates as $rate_key => $rate ){
    // Targetting "Flat rate"
    if( 'flat_rate' === $rate->method_id ) {
        $has_taxes = false;

        // Set the new cost
        $rates[$rate_key]->cost = $rate->cost * $rate_operand;

        // Taxes rate cost (if enabled)
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $tax > 0 ){
                // New tax calculated cost
                $taxes[$key] = $tax * $rate_operand;
                $has_taxes = true;
            }
        }
        // Set new taxes cost
        if( $has_taxes )
            $rates[$rate_key]->taxes = $taxes;
    }
}    return $rates;
} break; } } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.