Warning: Redis::connect(): connect() failed: Connection refused in /var/www/admclub.com/wp-content/object-cache.php on line 418
扩展Magento核心静态类的方法(1) | _站长俱乐部站长俱乐部


Magento - Extending Core Class

Recently I had to extend a Product Collection class of Magento to add another function, one way was to simply look for the core class and add the function there but it can cause problems when updating the code or changing the version so I thought to extend the class. It sounds fairly simple but Magento users will know very well that its really difficult since Magento does not provide much of a documentation. I found a couple of posts that were useful but none of them explained in a generic way so that reading their code you were able to extend that class but not some other. Here I would try to make it as generic as possible:

Let's say you want to make a widget which shows the products in desc order of the number of views, let's call it 'Most Viewed Widget'. For that you will need something like

$collection = Mage::getResourceModel('reports/product_collection');
$collection = $this->_addProductAttributesAndPrices($collection)

It works fine excluding the fact that if you view the code of the function you will see that it only shows you the products that are viewed at least one time and hence does not show any of your product that wasn't viewed at all (I needed those to be shown as well). Now let's try to extend this class to add our new function 'addViewsCountAll()' which will also include un viewed  products.

Step 1: Get the location of the core file:

As we know that all the core code is placed in 'app/code/core/Mage' so search for 'function addViewsCount' and you will find it in 'app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php'. (you will need this path later).

Step 2: Make xml file to let Magento know that you have added a module:

Go to app/etc/modules and add a file named Example_Collection.xml (where Example is the name of your folder placed in 'app/code/local/' and 'Collection' is the sub-folder of 'app/code/local/Example'.

Step 3: Make config.xml for your new class:

Go to app/code/local and make 2 folder in hierarchy as in step 2 e.g. app/code/local/Example/Collection, now make a folder named 'etc' in Collection folder and make a file named config.xml that will contain your class name and path, the path of the file will be 'app/code/local/Example/Collection/etc/config.xml'.

The file will contain the following xml for any new class extending original class, the difference will come between the model tag:
<?xml version="1.0"?>
..... //code depending upon class

Now you will need to add the path of your original class in two parts,

Part 1 will contain the name of the folder before the model and after up to (excluding) the last folder which contains the file.

Part 2 will contain the name of the final folder (which contains the file) and the name of the file itself (excluding extension).
Note that multiple directories of the 'Part 1' or 'Part 2' will be separated using '_'.
For our example the file path is 'app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php'

They will be added in xml file as follows
So, the final config.xml will look like:
<?xml version="1.0"?>

Note that the class name can be different but following naming convention it contains the path of its file in local directory.

Step 4: Making new class

Now finally after done with the configuration you can make your class and write all the functions you want. You can write new functions or overload existing ones (e.g. you may want to change the behavior of 'addViewsCount' to include un viewed products instead of writing new function).

Now go to 'app/code/local/Example/Collection' and make a folder named 'Model' followed by the folder(s) in 'Part 1' followed by folder(s) and file in 'Part 2'. In our case it would be

class Example_Collection_Model_Reports_Mysql4_Product_Collection
extends Mage_Reports_Model_Mysql4_Product_Collection
//your functions

Hope that helps.
If you are trying to extend an api for web service stay tuned as I will be writing a blog for that soon, though it is quite similar as well so you may try this meanwhile.



0 条评论。


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>