Magento Certified Developer GuideCategory: 8. CheckoutHow does Magento calculate shipping rates
Manish Prakash Staff asked 2 years ago

Shipping Methods in magento have the collectRates() function defined which is used to calculate the rates.

The collectRates() function return an object of type ‘shipping/rate_result’
A shipping method is grouped into carrier and method. A single shipping method will have one carrier and multiple shipping methods. e.g Fedex is a the carrier and Same Day Shipping, Express Shipping, Normal Shipping etc are different methods. So the way collectRates() works is

public function collectRates($request){
   
   $result = Mage::getModel('shipping/rate_result'); 
   if(!$error){
      //temp variable to check no error
  
   $method = Mage::getModel('shipping/rate_result_method'); 
   $method->setCarrier('code');
   $method->setCarrierTitle($this->getConfigData('title'));
   $method->setMethod('express');
   $method->setMethodTitle('Express Shipping');
   $method->setPrice(5);
   $method->setCost(5);
  
   $method1 = Mage::getModel('shipping/rate_result_method'); 
   $method1->setCarrier('code');
   $method1->setCarrierTitle($this->getConfigData('title'));
   $method1->setMethod('normal');
   $method1->setMethodTitle('Normal Shipping');
   $method1->setPrice(1);
   $method1->setCost(1);

   $result->append($method);
   $result->append($method1);
   }else{
            $error = $this->_getModel('shipping/rate_result_error');
            $error->setCarrier('code');
            $error->setCarrierTitle($this->getConfigData('title'));
            $error->setErrorMessage($this->getConfigData('specificerrmsg'));
            $result->append($error);
   }
   return $result;
}