1

In my attempt to fetching shipping rates, I want to override/rewrite this core file Mage_Sales_Model_Quote_Address to put my custom code. So that, I can call the original method using PHP's parent:: construct.

However, I found the Mage_Shipping_Model_Rate_Request object is created and used locally in the method Mage_Sales_Model_Quote_Address::requestShippingRates, hence I confused how to override/rewrite it.

public function requestShippingRates(Mage_Sales_Model_Quote_Item_Abstract $item = null)
{
    /** @var $request Mage_Shipping_Model_Rate_Request */
    $request = Mage::getModel('shipping/rate_request');
    $request->setAllItems($item ? array($item) : $this->getAllItems());
    $request->setDestCountryId($this->getCountryId());
    $request->setDestRegionId($this->getRegionId());
    $request->setDestRegionCode($this->getRegionCode());

    /** My custom code shall added in my override class */
    $request->setDestCityId($this->getCityId());
    $request->setDestDistrictId($this->getDistrictId());
    ...
}
2

In order to overwrite Mage_Sales_Model_Quote_Address, you need to add this code in your config.xml file

File : app/code/local/Namespace/Module/etc/config.xml

<global>
       <models>
          <sales>
              <rewrite>
                  <quote_address>Namespace_Module_Model_Quote_Address</quote_address>
              </rewrite>
          </sales>
       </models>
</global>

If you use this code inside config.xml, that means instead of Mage_Sales_Model_Quote_Address, your model class will be used by magento. If your custom model definition do not holds the requested method from default model, then magento is brave enough to use that method from original class.

Your model definition should look like this.

<?php
class Namespace_Module_Model_Quote_Address extends Mage_Sales_Model_Quote_Address {

    public function requestShippingRates(Mage_Sales_Model_Quote_Item_Abstract $item = null)
    {
       //ofcourse you can use here `Mage::getModel('shipping/rate_request')` if you need

       //no problems with that. 

       //this method will use by magento if `Mage_Sales_Model_Quote_Address::requestShippingRates` get requested
    }
}

So there is no doubt. You can rewrite this class and alter how requestShippingRates() method works right now. You can invoke Mage_Shipping_Model_Rate_Request class inside this function and still use it in your custom class

1
  • I wish to call the original method using parent:: construct in my rewrite model. So would be something like: public function requestShippingRates(...) { // add my own code parent::requestShippingRates(); }
    – Leongelis
    Aug 12 '14 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.