Okay, I have come up with a very simple hack that uses flat rate shipping as it already exists in the backend and provides customized per item shipping prices. It is very limited, but it does check for a shipping price assigned to a product, and if none exists it defaults to the normal flat rate shipping price. By overloading the flat rate model, it also allows for a handling fee to be applied to the overall order and limits to be set by country, etc. Basically, it has all the normal functionality of flat rate shipping with the added benefit of being able to define a custom shipping cost for individual products if you need to.
I am not a PhP programmer (yet), so I really want to thank Baseballtips for providing the code that allowed me to pull the shipping price from the product attribute table. I really, really needed to to be able to set a shipping price for each item in the store I’m working on. And, now that it works, I want to share it with others who might also find it useful.
Flat rate shipping is set up in the backend under “System > Configuration > Shipping Methods,” and this is where you will control what countries to apply flat rate shipping to, whether to apply a handling fee, etc.
We will be overloading this model : app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php
The reason we are overloading the core module is so you can upgrade without having your code get overwritten. If you don’t care about upgrades and prefer a simpler solution, you can just replace the function collectRates() in Flatrate.php with the code from that same function in my version of Flatrate.php in the next post once you have configured the “ship_price” attribute in xml.
First, go to “Catalog > Attributes > Manage Attributes” and add a new atribute with an Atribute Code of “ship_price”. Then go to “Catalog > Attributes > Manage Attribute Sets” and assign this new attribute to a Group. It is now ready to use, and you can go to “Catalog > Manage Products,” click on a product, select the Group from the left navigation, and enter a shipping cost for this item. Don’t forget to go to “System > Configuration > Shipping Methods” and enable Flat Rate Shipping and set the Type to “Per Item.”
Now, for the coding. First, go to “app/etc/modules” and add a new file called “MyNamespace_All.xml” (Anywhere in this post that you see “MyNamespace” - including in the code itself - sustitute your own made up namespace. For example, I use “SFA” for my namespace, so my file is named “SFA_All.xml") This file will include the following code (be sure to change “MyNamespace” to your actual namespace):
Now go to “app/code/local” and add a folder called “MyNamespace” (substitute your actual namespace). Inside this folder, add a folder called “ItemShipping”, then open that folder and add two folders - “Model” and “etc”.
Open “etc” and add a file named “config.xml” with the following code (be sure to replace “MyNamespace” with your actual namespace):
<?xml version="1.0" encoding="utf-8"?>
[continued in next post]