When it comes to customize the magento code, there are following 4 options:
1. Creating a observer should be the first to consider.
2. Extending and overriding the core modules should be the second to consider when option 1 cannot fails to accomplish the objectives.
3. Copy the core class to the local directory and put it in the same directory path it was in core directory, this is should be avoided.
4. Modify the core class directly, this should definitedly be avoided unless you are just trying to test a code snippet.
So, there are really only two recommended options which is to try to create an observer first, if it doesn’t work out then write a module to extend and override the core module.
Here is an example of how to create an observer in Magento. This module has two observer:
First one: When observed a product is added to the cart, print some messages.
Second one: To observe when a product review is submitted. The default Magento behavior is to save the product review with PENDING status, this observer will auto approve the submitted product review instead.
1. Create a xml file app/etc/modules/MyExtensions_Observerdemo.xml
<?
xml
version
=
"1.0"
?>
<
config
>
<
modules
>
<
MyExtensions_Observerdemo
>
<
active
>true</
active
>
<
codePool
>local</
codePool
>
</
MyExtensions_Observerdemo
>
</
modules
>
</
config
>
2. Create a xml file app/code/local/MyExtensions/Observerdemo/etc/config.xml
<?
xml
version
=
"1.0"
?>
<
config
>
<
modules
>
<
MyExtensions_Observerdemo
>
<
version
>0.1.0</
version
>
</
MyExtensions_Observerdemo
>
</
modules
>
<
global
>
<
models
>
<
myextensions_observerdemo
>
<
class
>MyExtensions_Observerdemo_Model</
class
>
</
myextensions_observerdemo
>
</
models
>
<
events
>
<
checkout_cart_product_add_after
>
<
observers
>
<
MyExtensions_Observerdemo_Model_Observer
>
<
type
>singleton</
type
>
<
class
>MyExtensions_Observerdemo_Model_Observer</
class
>
<
method
>addtocartEvent</
method
>
</
MyExtensions_Observerdemo_Model_Observer
>
</
observers
>
</
checkout_cart_product_add_after
>
<
review_save_before
>
<
observers
>
<
MyExtensions_Observerdemo_Model_Observer
>
<
type
>singleton</
type
>
<
class
>MyExtensions_Observerdemo_Model_Observer</
class
>
<
method
>autoApproveReview</
method
>
</
MyExtensions_Observerdemo_Model_Observer
>
</
observers
>
</
review_save_before
>
</
events
>
</
global
>
</
config
>
<global> applys to both frontend and backend
<fronend> applys only applys to frontend
<adminhtml> applys only applys to backend
3. Create a php file app/code/local/MyExtensions/Observerdemo/Model/Observer.php
<?php
class
MyExtensions_Observerdemo_Model_Observer {
public
function
addtocartEvent(Varien_Event_Observer
$observer
) {
$event
=
$observer
->getEvent();
//Gets the event
$product
=
$event
->getProduct();
$observermsg
=
"The event triggered>>> <B>"
.
$event
->getName() .
"</B><br/> The added product>>> <B> "
.
$product
->getName().
"</B>"
;
//Adds the message to the shopping cart
echo
Mage::getSingleton(
'checkout/session'
)->addSuccess(
$observermsg
);
}
public
function
autoApproveReview(Varien_Event_Observer
$observer
)
{
$event
=
$observer
->getEvent();
$reviewObjData
=
$event
->getData();
//print_r($reviewObjData);exit;
$reviewData
=
$reviewObjData
[
"data_object"
];
$reviewData
->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED);
echo
Mage::getSingleton(
'core/session'
)->addSuccess(
"Thank you for your input!!"
);
}
}