最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce - Flat rate shipping based on X quantity steps?

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Apr 12, 2019 at 0:13 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Apr 5, 2019 at 8:33 fish_rfish_r 1231 silver badge9 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 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.

Still can't comment so I need to post this fix to the above awesome answer.

For the rate calc to work properly, the return $rates; line must be outside the cart loop.

The complete function should be:

$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;
}

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; } } 
发布评论

评论列表(0)

  1. 暂无评论