We already talked about weight based shipping and in this post we found out how to charge different flat rates based on shipping weight thresholds.
But now I want to show you how you can use the default “Flat Rate” to calculate shipping costs based on cart weight, thanks to a multiplier. For example, your shipping rate might be “$5 for each Kg” – as you know the default “Flat Rate” only allows you to define one rate e.g. $10.
So, what if you want to calculate shipping charges by weight? Well, here’s a simple workaround for you.
PHP snippet: Turn Flat Rate into a Weight Multiplier Shipping Rate
Before getting into coding, a few notes first. The following snippet won’t work unless you:
- enter weight for every product in your store
- create a Flat Rate and get its ID (“84” in the example below)
- define your shipping by weight formula ($5 multiplied by rounded kilos in the example below. My snippet will return $5 if cart weight is 1.4Kg, $10 if cart weight is 1.6Kg, $15 if cart weight is 3.1Kg and so on… Adjust as per your project specifications)
The following snippet might not fully work if the Flat Rate is taxable and if you have tax enabled in your store. It might need a little tweak to make it work.
Also make sure to empty your Cart before testing – every time you work with shipping rates you need to clear your “session” and emptying the Cart should trigger that automatically.
/** * @snippet Flat Rate = Shipping by Weight * @how-to businessbloomer.com/woocommerce-customization * @sourcecode https://businessbloomer.com/?p=114302 * @author Rodolfo Melogli, Business Bloomer * @compatible WooCommerce 3.5.4 * @community https://businessbloomer.com/club/ */ add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 999, 2 ); function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { $cart_weight = WC()->cart->cart_contents_weight; if ( isset( $rates['flat_rate:84'] ) ) { $rates['flat_rate:84']->cost = 5 * round ( $cart_weight ); } return $rates; }
I was looking at how you can modify a shipping method from the core of woocommerce, and while reading the article, it occurs to me to ask you, maybe you know something, about a solution that I have been looking for a long time and cannot find.
For a project, I need to recalculate the tax.
Woocommerce by default calculates the tax according to the sale price, be it the regular price or the sale price, and allows to calculate the tax by applying the shipping cost.
I need it for a special project, it is to calculate the tax, on the cost price.
I have a plugin for cost price, and I need the tax to be calculated on the cost price, and not on the sale price.
And so, the tax based on the cost price should be shown in the cart and at checkout, instead of showing the tax on the sale price.
Do you think woocommerce could do this?
Kind regards,
Alex
Yes, with custom coding you can do anything you like!
Spot on – thanks brother
Great!
Hi Rodolfo,
Thank you for this article, it is getting me one step closer to what I am really needing for my client. Their courier charges a handling fee per product plus weight based shipping of anything over 5kgs. I see Joe did a work around to multiply per kg. My question is, Is there a way to incorporate the flate rate formula Price*[qty] and add the weight shipping rule. for instance 125*[qty]+1.5 per 1 kg over 5 kg.
Yes I’m pretty sure this can be custom coded
Very Good Really so happy,
I have a question, I have limited flat rates options so after the last option I would like to show a message to customers but its not working can you please help me.
Thanks
Hi Sami, you can’t echo inside an add_filter function. You’ll need a workaround
this code multiply amount per kg in cart
Nice 🙂
Great. It works.
What code to add if I want that if the Cart value is over a certain value (let’s say 75 $ or EUR) it will apply a free shipping option?
Thank you in advance.
Ciao Giacomo 🙂 You can do that via the settings: https://docs.woocommerce.com/document/free-shipping
How to get the weight from a group of products in a specific shipping class only? So the weight of only that products which have that one specified shipping class?
Hello Andre, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Greetings Rodolfo!
Great… you’re not so far from what I’m looking for 😉
I’m looking for add a fixed amount to the shipping costs. As example, if UPS plugin (which is connected to the UPS API) return $10 for the cart, I want to be able to add $2, so $12 for the shipping cost. (or a %). The operate (for 1% more + $2) can be ShippingCost = ShippingCost*1.01 + $2.
Maybe for the next post 🙂
Have a good continuation
Great posts!
Rémi
Thank you Remi!