2

I need to override Mage_Shipping_Model_Carrier_Flatrate through my custom module in magento 1.7.

Right now i have done this :

In my app/etc/modules/ i have the file "Namespace_Modulename.xml" with the code:

 <?xml version="1.0"?>
   <config>
      <modules>
        <Namespace_Modulename>
           <active>true</active>
           <codePool>local</codePool>
       </Namespace_Modulename>
     </modules>
   </config> 

In my app/code/local/Namespace/Modulename/etc/ i have file "config.xml" with the code:

 <?xml version="1.0" encoding="utf-8"?>
  <config>
    <modules>
       <Namespace_Modulename>
           <version>0.1.0</version>
       </Namespace_Modulename>
    </modules>
    <global>
     <models>
       <shipping>
         <rewrite>
              <carrier_flatrate>Namespace_Modulename_Model_Flatrate</carrier_flatrate>
         </rewrite>
       </shipping>
     </models>
   </global>
 </config> 

In my app/code/local/Namespace/Modulename/Model i have file "Flatrate.php" with the code:

  class Namespace_Modulename_Model_Flatrate
   extends Mage_Shipping_Model_Carrier_Abstract
   implements Mage_Shipping_Model_Carrier_Interface
   {

   protected $_code = 'flatrate';


    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    $freeBoxes = 0;
    if ($request->getAllItems()) {
        foreach ($request->getAllItems() as $item) {
            if ($item->getFreeShipping() && !$item->getProduct()->getTypeInstance()->isVirtual()) {
                $freeBoxes+=$item->getQty();
            }
        }
    }
    $this->setFreeBoxes($freeBoxes);

    $result = Mage::getModel('shipping/rate_result');
    if ($this->getConfigData('type') == 'O') { // per order
        $shippingPrice = $this->getConfigData('price');
    } elseif ($this->getConfigData('type') == 'I') { // per item
        $shippingPrice = '0.00';
        foreach ($request->getAllItems() as $item) {
            if (!$item->getFreeShipping()) {
                $productId = $item->getProduct()->getId();
                $productList = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('ship_price')
                    ->addIdFilter($productId);
                foreach($productList as $product) {
                    $quantity = $item->getQty();
                    if ($product->getShipPrice()) {
                        $shippingPrice+=$product->getShipPrice() * $quantity;
                    } else {
                        $shippingPrice+=$this->getConfigData('price') * $quantity;

                    }
                }
            }

        }
    } else {
        $shippingPrice = false;
    }

    $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);

    if ($shippingPrice !== false) {
        $method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('flatrate');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('flatrate');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
            $shippingPrice = '0.00';
        }


        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);
    }

    return $result;
}

public function getAllowedMethods()
{
    return array('flatrate'=>$this->getConfigData('name'));
}

}

But it's not working. May be i am missing something or doing anything wrong. Please help,

Thanks in advance, Sarvagya

5
  • 2
    It worked for me. Just copy/pasted the code you provided, I added an 'echo "OK";' on the first line in the model. Then I added a product to the cart and clicked on estimate shipping. I got the "OK" message. This means the file is loaded. Make sure you cleared the cache.
    – Marius
    Commented Mar 20, 2013 at 15:03
  • What @Marius said. You may also have another module already overwriting the model - this is one of Magento's main design flaws. Commented Mar 20, 2013 at 20:59
  • Check in Config > Advanced and see if your module is installed. Maybe you forgot the modules/etc/Namespace_Modulename.xml file
    – philwinkle
    Commented Mar 21, 2013 at 18:58
  • 1
    Did you found your problem? What was it? Please provide an answer an mark it as solution. Commented Apr 7, 2013 at 1:13
  • I have copied this exact code to my custom module. When I open the shopping cart page I get this error: Fatal error: Call to a member function setStore() on a non-object in ../app/code/core/Mage/Shipping/Model/Shipping.php on line 421
    – A.W.
    Commented Sep 13, 2013 at 7:04

1 Answer 1

2

First, clear cache and disable/refresh compilation. Then, rule out a competing rewrite. Deactivate your module and do the following on a page or with a workbench script:

$className = Mage::getConfig()->getModelClassName('shipping/carrier_flatrate');
echo $className;
$class = new $className;
echo get_class($class);

You should see Mage_Shipping_Model_Carrier_Flatrate two times. Anything other class name and you have a multi-rewrite to chain or otherwise resolve. If you do not see the classname a second time then there is an issue instantiating the class (likely because of autoloading issue). Reactivate your module and test the factory class name resolution as above, and you should see your Flatrate class name.

A final note - if you are rewriting the core Flatrate model, convention indicates that you should extend from it. It's academic in this case given that you are overriding everything in the parent class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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