Stuff & Nonsense

Category based shipping in magento

One of the things that’s always annoyed me about Magento is the dearth of decent, free shipping modules available for the system. Out of the box you can do a reasonable amount with regards shipping, setting a flat rate or using one of the built in shipping methods…but if you want to do anything fancy (and in my experience, people always do) then you need to pay out for one of the many customisable shipping modules available.

It’s been nearly a year since I last looked at Magento, but a recent freelance project has meant me getting back into it and as the client has some very specific shipping requirements, I thought I’d write my own shipping module. I spent a few hours fiddling with config files, before rememebering why Magento always annoyed me so much (hint, it’s the config files) and used the rather brilliant magento module generator to get me started.

Here’s what I wanted from a shipping module:

  • Different shipping rates for individual products
  • Different shipping rates to different countries
  • Easy assigning of shipping rates to products

So, to tackle these problems I came up with a module that uses Shipping rules defined through categories. This means you can setup your rules, and then use the standard product / category concept in Magento to assign your shipping rates.

Once you install the module (see below for a download) each category gets a new attribute under its ‘general’ tab, named ‘Shipping’. That’s where you enter your rules. Rules are extremely simple, and consist of a series of lines, each of which is made up of ‘country code’ = ‘price’

here’s an example of one ruleset:


This translates to ‘If the delivery address is in the UK, free shipping. If it’s in the European union, shipping is 3.00, for the rest of the world it’s 5.00’.

You can have as many lines as you want (they’re processed top to bottom) and if you have multiple items in your basket that produce different shipping rates, the most expensive rate is picked (there’s no option to sum the rates together yet, I might add that in at some point).

You can add these rules to your actual, published product categories or do what I’ve done for this client and set up a seperate tree of ‘Shipping categories’ like so:


So there you go. Bit basic, but does what I needed it to do.

Here’s the download link (upload the ‘app’ folder from the archive into the root of your magento installation):

[wpdm_package id=’984′]

30 thoughts on “Category based shipping in magento

  1. Just wanted to extend my thanks; this is exactly what I was looking for and couldn’t find as all the extensions on Magento Connect are way too complicated for what I needed. Excellent! Thank you!!

  2. Hi Paul,
    thanks for sharing your module, could you please tell me for which magento edition it is safe to use? I have CE 1.7 edition. Thanks again, Stavros

  3. Hello & thanks for the help. Is there anyway to make this module do this:

    I want only flat rate shipping per attribute set (or category). No matter the quantity of a certain attribute set (or category) is purchased, the flat rate applies as long as all items are of this certain attribute set (or from these certain categories). If items from different attribute sets (or categories) are also in the cart, then the shipping method defaults to the UPS (or other carrier) method.?

    I say attribute set or category because in my case it doesn’t matter if I choose by attribute set or by categories, so either would work for me.

    Thanks again!

  4. Hi, can i use it for differets states, i need different shipping but only for one country, and some states.

  5. I run magento community

    I offer free shipping on all products right now

    I want to add a free sample in my store for which the customer only needs to pay the shipping amount. Table rates are not working.

    Also when the customer is buying the regular products , the paid shipping option should not show up in checkout process.

    Will your module help me with this problem ?

  6. that looks great. Can you tell me how it interprets it when one product is in different category, say, I have a headphone in ‘Smartphones accessories’ and in ‘Computer accessories’. My shipping rules are
    – 1.00 in ‘Smartphone accessories’
    – 2.00 in ‘Computer accessories’

  7. Hi. If you add if ($request->getFreeShipping() == true) { $rules_result = ‘0.00’; } on file app/code/local/Tallpaul/CategoryShipping/Model/Carrier/Categoryshipping.php the free shipping will start working with your module

    1. Nice one Pablo. Just spotted your post..
      Found i got errors with { $rules_result = ‘0.00’; } due to the .00
      but ammended to { $rules_result = ‘0’;} and works great !

  8. I think you have a minor error in collectRates:

    I’ve added the second line to the following as $rules could be empty and it causes an error in the third line:

    $rules = $category->getData(‘shipping’);
    if ($rules == ”) {break;}
    $rule_lines = explode(“\r\n”,$rules);

    Btw, I’m trying to merge this with table rates. I’d really appreciate any help you might be able to give me with my question:

    Thanks for your extension which is really useful.

    1. Wondering if you ever managed to acheive this Pedro.
      Im basically looking at implementing Paul’s module, but additionally offering free shipping on orders above £100 using table rates.
      Any advice?
      Best, Bob

      1. Hi Bob, I can probably frig this for you in my module, I can take a look at it this evening.

        In the continuining hackiness of this solution I’m envisioning adding rules like this:


        ie: if country is GB and subtotal is greater than or equal to 100, shipping price is 0. if country is GB, shipping price is 5 etc etc.

        Would that work for you?

        1. Thanks for the speedy reply.
          I implemented a shopping cart rule for the over £100 mark,
          and followed Pablos suggestion above:
          if ($request->getFreeShipping() == true) { $rules_result = ‘0’; }

          And found this works a treat.
          Though your solution could be better since the free shipping rules could apply differently to different catagories with ease.
          Can you forsee any real problems using the simpler shopping rules and pablos additional code line?

  9. Hi Paul,
    great looking plugin you’ve built for magento shipping.
    Looks like the ideal candidate for my needs..
    The download link unfortunately no longer works, Any chance of a fresh link?

    Thanks youuu!

    1. Apologies.
      Oddly, in wirting this response , the download link seems to have updated itself correctly!

      nice one Paul, looking forward to having a good scope through the code, no doubt ill need to modify it slightly to my needs.

  10. I know it’s several years since you posted this but in the case that you’re still following up comments..

    In addition to setting shipping rates per item, a module that allows you to EXCLUDE certain items from certain countries is what I need. Does your extension allow you to do this ?

    1. Hi Lydia, as you say I haven’t looked at this for a while, but if you use a price of ‘-1’ in one of your category rules, that should disallow shipping to that country.

      If you want any help with this, or with magento in general I am available for contract work, you can email me at ‘[email protected]

    1. Hi, unfortunately not. I wrote this code several years ago now and haven’t needed to modify it since. I’m sure any decent magento developer would be able to do this for you though.

  11. Thanks, code works well for magento 1.9.2.

    I want to know how I can add some specific words in front of the shipping fee? For example, I have two categories, one category is overnight shipping(worldwide=45.00), the other category is normal shipping(worldwide=25.00); In this way, products in overnight shipping category must use $45 overnight shipping and products in normal shipping category must use $25 2nd day shipping.

    Can I make the label to be “Fedex Overnight Shipping Fee 45” and “Fedex 2nd Day Shipping Fee 25”? Currently, it only shows “method name + 45/25”.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.