Magento: Calculate Table Rates per item/product

Published 2012-11-25 · Archived article

Magento does not provide the ability to calculate shipping charges per item outside of flat rates. For businesses shipping large items via Freight or in individual boxes, shipping charges need to be on a per item basis.

The code below will allow you to specify shipping costs per item based on weight. In our implementation we have 3 individual shipping rates per state. Standard Freight (100lb), Large Freight (500lb), and Extra Large freight (1000lb). Actual weight is irrelevant – we just need to ensure we can accurately quote shipping on 3 sizes to 48 destinations.

Table Rate shipping per item

Using the code below shipping will be calculated per product. Shipping is not calculated per product but per item – if item A costs $10 to ship, and a cart contain (10) item A’s, shipping will be $100.

  • Open /app/code/core/Mage/Shipping/Model/Carrier/Tablerate.php
  • Find:
    $result = Mage::getModel('shipping/rate_result');
    $rate = $this->getRate($request);
  • Add After:
    		 $combined_request = $request;
            $individual_rate = 0;
            foreach($combined_request->getAllItems() as $item)
            {
                $combined_request->setPackageWeight($item->getRowWeight());
                $temp_rate = $this->getRate($combined_request);
                $individual_rate += ($temp_rate['price'] * $item->getQty());
            }
            $rate['price'] = $individual_rate; 

I can’t remember where this code originated, unfortunately, but it was originally shared by another Magento developer. This code could easily be adapted to Matrix Rates if your already using it.

Table Rates XML file containing all US States:

— Download Here

Discussion

Previously published comments, retained for reference. New comments are closed.

jeremy

This looks great. But are you sure it's best practice to modify core code modules? There are free extension that can achieve this without the need for code edits.

Chris Rutherford

You could easily create your own extension using the code example in the post. I'm not aware of the extension, what is the name of it?

Sandy

I really need that feature in my store. But I can't create an extension myself. I didn't find any good free extensions mentioned in the previous comment. For now the best solution I found is a paid extensions, shipping per product by amasty. It solves the problem

Ankur Jain

thanks for the solution it worked for me, but i used the logic in different way :)