Add an additional cost to flat rate shipping each 3 items in Woocommerce
Asked Answered
M

2

7

I'm running a woocommerce shop and using a Flat Rate shipping $15. I have written a formula to add $1.25 for each additional item.

13.50 + ( 1.25 * [qty])

Sipping "flat rate settings | $1.25 for Additional Each Item:

$1.25 for Additional Each Item

But I want to add this cost $1.25 for every 3 items. I mean 3, 6, 9, 12 and so on...

Can anyone tell me how to do this? Any help is appreciated.

Manrope answered 4/7, 2018 at 3:23 Comment(1)
can you use floor()? 1.25 + floor(qty/3)Astyanax
A
4

Updated (2021)

The following code will add an additional cost to flat rate shipping method each 3 items (3, 6, 9 …).

You will need to change your shipping cost with a simple initial cost instead of your formula.

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

The code (where you will set your additional shipping cost):

add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 10, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE set your additional shipping cost
    $additional_cost = 1.25;
    $items_count     = 0; // Initializing
    $each_items      = 3; // Number of items (for additional cost)

    // Loop through cart items for the current shipping package        
    foreach( $package['contents'] as $cart_item ) {
        $items_count = += $cart_item['quantity']; // Count cart items for current shipping package
    }

    if ( $items_count >= $each_items ) {
        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            // Targetting "flat rate"
            if( 'flat_rate' === $rate->method_id ){
                $initial_cost = $new_cost = $rate->cost;
                $has_taxes    = false; // Initializing
                $taxes        = array(); // Initializing
                
                // Adding to cost the additional cost each 3 items (3, 6, 9 …)
                for($i = 0; $i <= $items_count; $i+ = $each_items){
                    $new_cost += $additional_cost;
                }
                $rates[$rate_key]->cost = $new_cost; // Set the new cost
    
                // Taxes rate cost (if any) - Loop through taxes array (as they can be many)
                foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $tax;
                        // Get the tax rate conversion
                        $tax_rate    = $initial_tax_cost / $initial_cost;
                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax changes
                    }
                }
                // set array of shipping tax 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.

Don't forget to disable "Enable debug mode" option in shipping settings.


Answer based on your 2nd comment:

you will replace this block:

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
    $new_cost += $additional_cost;
}

by the following:

// Adding to cost an additional fixed cost for the 2nd item
if($items_count >= 2){
    $new_cost += 6.21; 
}

// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
    $new_cost += $additional_cost;
}
Aplanospore answered 4/7, 2018 at 4:30 Comment(4)
Loic, is this possible if a customer add to cart a single product and the shipping amount will be $9.99 but when he adds another item the shipping cost would be jumped to $16.20 and then shipping cost would be added each 3 items by $1.45 as currently doing this function: I can handle the shipping cost by this part of your function: // HERE set your additional shipping cost $additional_cost = 1.25; $items_count = WC()->cart->get_cart_contents_count(); Advanced Thanks!Manrope
All is working fine! The first item shipping is $9.99 and when I add Qty.2 The shipping price jummps to desired cost $16.20. But When I add Qty. 3, it returns to the first state at $9.99 shipping cost and then works as previously working. I used your both condtions as instructed. Thanks for the reply.Manrope
Hi, can we display $5 of handler fee below the shipping cost of $9.99, if the quantity of the item is [1] and this would be added into total? Thank you.Manrope
I have add updated the code at the end for your 2nd comment… For your last comment, It's not clear. You should please better ask a new question.Aplanospore
A
-1
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
    if ($i % 3 == 0){
        $all_number = $all_number + 1;
    }
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>
Alli answered 4/7, 2018 at 5:15 Comment(2)
why don't you add some explanation to your answer it will be more helpful.Vargas
Maulik, thanks for your reply too but above function worked for me :)Manrope

© 2022 - 2024 — McMap. All rights reserved.