Search code examples
phpmagentomagento-1.9

overriding price model in magento


I am facing problem in overriding product-> type -> price.php model in Magento.

Here is my app/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <softweb_catalog>
        <active>true</active>
        <codepool>local</codepool>
    </softweb_catalog>
  </modules>
</config>

Here is

app/code/local/Softweb/Catalog/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
    <softweb_catalog>
        <version>0.1</version>
    </softweb_catalog>
</modules>
<global>
    <models>
        <catalog>
            <class>Softweb_Catalog_Model</class>
        </catalog>
        <catalog>
            <rewrite>
                <product_type_price>Softweb_Catalog_Model_Product_Type_Price</product_type_price>
            </rewrite>
        </catalog>
    </models>
</global>
</config>

Now Price.php path is app/code/local/Softweb/Catalog/Model/Product/Type/Price.php

 class Softweb_Catalog_Model_Product_Type_Price extends Mage_Catalog_Model_Product_Type_Price
 {

   public function getPrice($product)
   {
      die('function called');
   }

  }

I don't know what I am missing....

PS I am using magento 1.9.0.1


Solution

  • The problem resides here

    <models>
         <catalog>
              <class>Softweb_Catalog_Model</class>
         </catalog>
    ....
    </models>
    

    Here you are defining your model. catalog is the reference that you are providing for your module's model. Suppose you have a file Foo.php inside your module's model directory. That is

    Softweb
    |
     ----------Catalog
               |
                ----------- etc
               |             |
               |              ---------- config.xml
               |
                ----------- Model
                            |
                             ----------- Foo.php
    

    And suppose your Foo.php holds a method Foo() method.

    <?php
    class Softweb_Catalog_Model_Foo extends Mage_Core_Model_Abstract
    {
          public function Foo()
          {
               //some foo codes here
               //returns something
          }
    }
    

    How can you get this Foo() method that is defined inside your model? According to your model definition it should look like this

     $foo = Mage::getModel('catalog/foo')->Foo();
    

    However your model reference should be unique. So you cannot use catalog for your model reference. Since it is already using in Mage_Catalog core module. See this

    Location:app/code/core/Mage/Catalog/etc/config.xml
    <global>
            <models>
                <catalog>
                    <class>Mage_Catalog_Model</class>
                    <resourceModel>catalog_resource</resourceModel>
                </catalog>
                ---------
            </models>
            --------
    </global>
    

    So if you need to use your model, you should have a unique reference for your model. That is

        <models>
            <softweb_catalog>
                <class>Softweb_Catalog_Model</class>
            </softweb_catalog>
            <catalog>
                <rewrite>
                    <product_type_price>Softweb_Catalog_Model_Product_Type_Price</product_type_price>
                </rewrite>
            </catalog>
        </models>
    

    Here your module is referenced using softweb_catalog and is unique. So now you can access Foo()method as like this.

      Mage::getModel('softweb_catalog/foo')->Foo();
    

    Also it rewrites your required Model file. But since your module does not hold any model files, there is no need for this code. You only need this.

    <global>
         <models>
           <catalog>
              <rewrite>
                 <product_type_price>Softweb_ConstPrice_Model_Price</product_type_price>
              </rewrite>
           </catalog>
         </models>
      </global>
    

    It will allow you to rewrite Mage_Catalog_Model_Product_Type_Price class. Hope that will help you to understand the fault in your code.