How to adjust shipping fee:
– based on quantity
– on total amount in cart.
Adjust shipping fee based on quantity in cart.
WooCommerce.
I have a customer who is planning on selling her newest book “Hverdagslykke” (Everyday Happiness). It is in Norwegian.
Shipping for 1 bok (1 book) = 85 Kr.
Shipping for 2 eller flere bøker (2 or more books) = 120 Kr.
Result quantity of 1:
Result quantity of 2 or more:
I am using the code on this site: sjelefode.no (sjelefode = food for the soul.)
I have been searching for a way to adjust shipping charges that change when quantity changes.
I have been following this tutorial:
https://businessbloomer.com/woocommerce-setup-tiered-shipping-rates-order-amount
In the backend.
WooCommerce -> Settings -> Shipping -> the customers shipping zone is Norway. I hovered over the Zone name Norway and using the quick edit bar selected Edit. Here I adjusted 1 shipping method and added a new title and Cost of 85. Then Made another shipping method where I also adjusted the title and added the Cost of 120.
NB! If your not seeing the Shipping tab then go to General and check the Shipping location(s) option is not disabled. Then go to Shipping and add a Shipping zone or zones. If you want to rerun the setup wizard then go to Products. Click the help tab and Setup Wizard. docs.woocommerce.com/document/woocommerce-setup-wizard/
NB2! If you have added a specific shipping zone and your shipping titles are not showing up or shipping is not showing up then they might not have been added. Try to add them to “Locations not covered by your other zones”. If they show up here then you will need to check the shipping zone regions as something might be missing from here.
I then went to functions.php and pasted the following code:
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 ); function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { $threshold = 2; if ( WC()->cart->get_cart_contents_count() < $threshold ) { unset( $rates['flat_rate:6'] ); } else { unset( $rates['flat_rate:1'] ); } return $rates; }
NB! To find the correct flate rate ids. In the frontend in the cart. Right click the shipping and select Inspect (Element) to see the HTML and CSS used on the page. From inspecting I could see that the flat rate id is 6 for more then 1 book. To find the flat rate for 1 book I would then adjust the quantity in the cart and then right click shipping to find the correct flate rate id here as well.
A tip.
Sometimes the cache can be really slow to refresh.
1. To refresh WooCommerce go to in the backend: WooCommerce -> Status -> Tools. Then just click to clear transients. (I just clicked the various buttons to be sure that the cache was cleared.)
2. If needed open Inspect. Right click the browser and open Inspect. Move the cursor on top of the refresh half circle icon (top left beside the URL line) and hold down the mouse button and select “Empty Cache and Hard Reload.”
The above techniques should clear the cache so you can see the newest adjustment.
Adding another shipping option.
We figured that we had to add another shipping option to the cart.
flat_rate:1. 1 book = 85kr
flat_rate:6. 2 books = 120kr
New shipping option: flat_rate:8. 3 or more books = 175kr
I added the 3rd shipping option in the similar way to what I did further above.
Here is the new code.
// Change shipping based on quantity purchased. add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 ); function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { $threshold1 = 2; $threshold2 = 3; if ( WC()->cart->get_cart_contents_count() < $threshold1 ) { unset( $rates['flat_rate:6'], $rates['flat_rate:8'] ); } elseif ( WC()->cart->get_cart_contents_count() < $threshold2 ){ unset( $rates['flat_rate:1'], $rates['flat_rate:8'] ); } else { unset( $rates['flat_rate:1'], $rates['flat_rate:6'] ); } return $rates; }
An explanation of the code:
$threshold1 = 2. Value of 2.
$threshold2 = 3. Value of 3.
if ( WC()->cart->get_cart_contents_count() < $threshold1 ) {
= If cart contents count is less then 2.
unset( $rates[‘flat_rate:6’], $rates[‘flat_rate:8’] );
= Unset/remove shipping option 2 books and 3 or more books. The 1 book option remains and is seen in the cart.
elseif ( WC()->cart->get_cart_contents_count() < $threshold2 ){
= If cart contents count is less then 3.
unset( $rates[‘flat_rate:1’], $rates[‘flat_rate:8’] );
= Unset/remove shipping option 1 book and 3 or more books. The 2 books option remains and is seen in the cart.
else { unset( $rates[‘flat_rate:1’], $rates[‘flat_rate:6’] );
= The If and elseif checks have been for less then 2 and less then 3.
Unset/remove shipping option 1 book and 2 books. The shipping option of 3 and more books remains and is seen in the cart.
NB! If I needed additional shipping options I would also add additional elseif checks.
Adjust shipping fee based on total amount in cart.
Based on Richards question below in the comments I added another method to adjust the shipping fee.
This for total amount in the cart.
- Create your shipping methods. Here are the ones I created.
Add a title you want to have seen in the cart. For this example I added the title of
0 – 2000 with a cost of 100
2001 – 4000 with a cost of 75
4000+ with a cost of 50
Clicking Edit while hovering over the title I can then adjust method title, tax status and cost of the specific flate rate.
2. Check to see that all the shipping methods are seen in the cart.
3. I added the following code into the functions.php file based on the flate rate id I found for each of the shipping methods.
/* Change shipping based on total cost purchased. */ add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 ); function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { $threshold1 = 2000; $threshold2 = 4000; if ( WC()->cart-> cart_contents_total < $threshold1 ) { unset( $rates['flat_rate:9'], $rates['flat_rate:10'] ); } elseif ( WC()->cart-> cart_contents_total < $threshold2 ) { unset( $rates['flat_rate:8'], $rates['flat_rate:10'] ); } else { unset( $rates['flat_rate:8'], $rates['flat_rate:9'] ); } return $rates; }
flate_rate:8 = 0 – 2000
flate_rate:9 = 2001 – 4000
flate_rate:10 = 4000 +
When threshold1 is under 2000 flate rate 9 and 10 are removed. Leaving flate rate 8 with a shipping fee of 100.
When threshold2 is under 4000 flate rate 8 and 10 are removed. Leaving flate rate 9 with a shipping fee of 75.
If it is not below 4000 then flate rate 8 and 9 are removed. Leaving flate rate 10 with a shipping fee of 50.
Resources:
businessbloomer.com/woocommerce-setup-tiered-shipping-rates-order-amount
stackoverflow.com/questions/22249615/get-woocommerce-carts-total-amount
code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce–cms-26098
Thank you very much!!! your contribution is very usefull!
I have one question, for example, in the code “Adjust shipping fee based on quantity in cart” I have 8 shipping rates range. But I have 5 shipping zones with diferent shipping rates ranges…
is possible modify this code or I need other solution?
I do created 8 flat rate for eachshipping zone. In this moment when the client go to the shopping cart in shipping shows all possible prices of shipping for his zone> My problem is with the filtre.
Each flat rate has a diferent ID for any shipping zone.
Sorry form my english.
Thank you in advance, Ramiro
Hi! Thank you very much for the code. I tried to use it quite differently but didn’t work (in my case!).
The idea is the following:
Downtown: free shipping for orders > $1000. If less, shipping rate is $100
Suburbs: free shipping for orders > $1500. If less, shipping rate is $150
South zone: free shipping for orders > $2000. If less, shipping rate is $200
Rural area: free shipping for orders > $2500. If less, shipping rate is $300.
These are not cities nor countries. Just zones within a couple of cities. Not even postal codes.
Do you think it’s possible to achieve with your examples? Thanks!! Daniel
Hi,
I sell multiple products on my website. I would like to use your flat rate shipping depending on the number of items in cart, but on a specific product only.
Ex:
Shipping 1 book = 85kr
Shipping 2 book = 120kr
Shipping 1 book and one DVD = 85kr (same as one book only).
Can you help me make it work?
Thank you.
Hello
Thank you for your beautiful site
I always check the content
it was very useful
thank ….
Hey Paal. I tried to follow your tutorial on Shipping Prices for Woocommerce but I can’t get my particular needs to work. Any help would be greatly appreciated, happy to pay you. Thanks very much
I just have three shipping options.
£0 – £2000 = £100
£2001 – £4000 = £75
£4000 + = £50
Hello Richard
I extended the tutorial with your question.
If you or anyone else would like to donate for helpful advice received.
Please do so through PayPal and use this e-mail address: paaljoachim@hotmail.com
Thanks.