Progressive quantity based shipping costs with a max cost in Woocommerce
Asked Answered
C

2

5

(How) can I set a max_fee for flat fee shipping costs in WooCommerce?

Adding this to the costs field in the flat fee settings of a shipping method works:

(1 * [qty]) + [fee min_fee = "2,5"]

(i.e. shipping costs 2,5 per order + 1 Euro per product) -> at least 3,5 and increasing + 1 for each product).

However, I want to charge 3,50 at minimum (for 1 item) and 4,50 at max (for 2 or more items). I.e. functionally this formula: (3,5 * [qty]) or [fee **max_fee** = "4,5"]

Is this possible? / What is the correct syntax to use? I cannot find this anywhere. I only found this reference ("add max_fee shortcode attribute for flat rate" / "allow max_fee in addition to min_fee in flat rate costs fields") which seems to indicate that a feature request for a max_fee for flat fees (other than for percentages) is implemented. But I can't get it to work.

Claudette answered 28/12, 2018 at 14:34 Comment(0)
A
5

You can't use available settings to charge a variable shipping cost as defined:

  • 3,50 for the first item
  • 4,50 for more than one item (2 or more)

So you will need to use the following custom hooked function:

add_filter('woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );
function custom_shipping_costs( $rates, $package ){
    // Get cart items count
    $items_count = WC()->cart->get_cart_contents_count();

    // Your cost settings
    $new_cost = $items_count > 1 ? 4.5 : 3.5;

    // Loop through shipping methods rates
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;

        // Targeting "Flat rate" shipping methods
        if ( 'flat_rate' === $rate->method_id ) {
            $default_cost = $rates[$rate_key]->cost;

            $rates[$rate_key]->cost = $new_cost;

            $rate_conversion = $new_cost / $default_cost;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    $taxes[$key] = $tax * $rate_conversion;
                    $has_taxes = true;
                }
            }
            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.

Once the code is saved, go in your flat rate settings and set the rate cost to 1.00:

enter image description here

Now the shipping rate will change from 3.50 (for one item) to 4.50 (for more than one items)

Augustin answered 28/12, 2018 at 17:0 Comment(1)
I have implemented your solution and it is perfect, many many thanks!!!Claudette
N
1

Easiest way I can think of is to approximate a custom function using a tool like: https://mycurvefit.com/

Aim to get a function that approaches some limit by specifying data points - then the tool approximates a function to fit this data. This solution will not result in regular pretty fees, but would be a quick solution, if you don't want to get in the code.

Nummular answered 25/2, 2023 at 8:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.