Today’s WooCommerce Tutorial contains some fairly high level code which will allow you to do something really specific, that can help with getting your pricing just right, for example, if you’re using a shipping API which is returning weight based calculations.
The Use Case
I created this code for a client who was finding certain low weight items were coming in under a shipping API threshold, because the item was given a weight that did not include packaging.
In this client’s case, the items themselves are sold as “low weight” so it was not desirable simply to add extra weight onto the shipping weight (which was displaying in the site as additional information).
Therefore, in order to have shipping weight always calculate, the “packed weight” needed to be set per product.
The actual maths – in this case – and therefore this snippet, were pretty straightforward, but of course, please do adjust according to your own needs.
Remember to Test This
As always, test first. Then test in staging. Fire a few carts at it and see if you get the shipping rate you wanted to return.
Add Additional Shipping Weight to Cart Items in a Shipping Class
Find the code in the Gist below:
What to replace?
You should replace the following elements with items relevant to your own use case:
- $additionalweight = 6; – replace 6 with some other amount!
- “shipping-class-slug” – replace this with the actual shipping class slug you will use in your site
Where to put this?
Add this snippet into either – in order of my personal preference:
- A custom plugin (best)
- A snippets plugin (good)
- The functions.php of your child theme (not so good)
- functions.php of your current active theme (never if this is not a child theme!)
Questions, Comments, Praise, Flames?
Leave a comment below if this worked, didn’t work, or if you extended this in some awesome way. Share the knowledge. Share the love.
1 thought on “WooCommerce Tutorial – Add Additional Shipping Weight to Cart Items in a Shipping Class”
Great code, thanks! One question: why do you deem using the functions.php of your child theme as “not so good”?