Magento News

 

Overriding Core Models with a Custom Module

Magento’s architecture is module-based, which means that you can easily modify existing and add new functions by creating your own modules. This how to will give you a short overview over the steps you need to take.

0. Used example
For this howto, I will create a module that overrides the “GetPrice()” function, which returns the price for a given product. I want the price to be always 50. Obviously that won’t make sense in a real store, but it is a fairly simple task so I decided it would be right for this first howto.

1. Create directories

First of all, you need to create some directory structure that will hold your module files. Go to /app/code/local and create a directory representing your’s or your company’s name. All your modules will go into that directory. I will use this for the example: /app/code/local/MyCompany.

Now we need to create the module directory. The module directory represents the name your module gets. I will use /app/code/local/MyCompany/ConstPrice in this example.

Every module needs a configuration file. This file is located under the etc/ subdirectory, so you need to create that directory as well. In my example it is /app/code/local/MyCompany/ConstPrice/etc.

The last needed directory is the Model/ subdirectory. I will explain what this is for later.

2. Build the config.xml
Now that we have all the directories we need, we can create a new file named config.xml inside the etc/ directory. This file will be used by the Magento module system to know what your module does and where the needed files are located. Here is the config.xml that we will need for our example:

<?xml version="1.0" encoding="utf-8"?>
 <config>
  <modules>
   <MyCompany_ConstPrice>
    <version>0.1.0</version>
   </MyCompany_ConstPrice>
  </modules>
 <global>
  <models>
   <catalog>
    <rewrite>
      <product_type_price>MyCompany_ConstPrice_Model_Price</product_type_price>
    </rewrite>
   </catalog>
  </models>
 </global>
</config>

As you can see, we will override the “Product/Type/Price.php” model inside the “Catalog” Module. Note that the underscores in represent the directory structure the core model is in. This is the same with MyCompany_ConstPrice_Model_Price, which points to MyCompany/ConstPrice/Model/Price.php.

3. Create the Model
Now we need to create the model. As you can see in the config.xml file, we have to create a new file named Price.php in the Model/ subdirectory. Here is the content of the file:

<?php
  class MyCompany_ConstPrice_Model_Price extends Mage_Catalog_Model_Product_Type_Price
  {
    public function getPrice($product)
    {
      return 50.00;
    }
  }

As you can see, we create a new model class which extends the core model class. that means your new class will have all the properties and functions the core class has. all functions you put in your class will be used instead of the core classes functions, and your can also add completely new functions to it. For now we just replaced the getPrice() function, so it will alway return 50.00.

4. Load your module
Now we finished our module, but Magento doesn’t know of it’s existence yet. We have to create a file in /app/etc/Modules that loads our module. Name that file MyCompany_All.xml and put this in it:

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

All xml files inside /app/etc/Modules will be loaded automatically. By placing the above file in that directory, Magento will know that is has to look for a config.xml in /app/code/local/MyCompany/ConstPrice/etc and load your module.

Source: belanur.de

class MyCompany_ConstPrice_Model_Price extends Mage_Catalog_Model_Product_Type_Price
{
public function getPrice($product)
{
return 50.00;
}
}

Possibly Related Posts:


 

Leave a Reply