I am creating a shipping module that will use table rates for determining the amount to be charged for shipping. My question is, do I use the collectRates method in the shipping module to display the table rates, or what do I do in this scenario?
Any insight, ideas, and/or suggestions are greatly appreciated.
Thank you!
Best Answer
collectRates()
is called for each shipping module.It receives as parameter an object instance of
Mage_Shipping_Model_Rate_Request
. Let's call it$request
.Your
collectRates()
method should start like this:This means 'do nothing if the shipping method is disabled'.
Through the
$request
parameter you have access to all the products in the cart:$request->getAllItems()
.You can get the total weight of the cart like this:
You can get the total number of products in the cart (qty):
You can get the shipping address details as follows:
These are the most important ones. For the additional info see the methods listed at the top of this class
Mage_Shipping_Model_Rate_Request
.Based on this info you can calculate a price (or more prices) for delivery.
After you have the prices your
collectRates
method should return an instance ofMage_Shipping_Model_Rate_Result
.For this create an instance:
Each result can support different methods for different prices. For each method you need to do the following:
After finishing adding your methods simply return the result: