====== Customizing Magento using Event-Observer Method ====== ===== Overview ===== Apart from the powerful OOP way of customizing Magento which is overriding methods by subclassing Magento's core Blocks and Models, there is another way to plug in customizations in key flow areas of your Magento eCommerce shop. Referred to as the Event-Observer methodology, Magento has been programmed to raise events in crucial areas of the flow and handling these events for customizations would keep upgradation a simple task that does not require fiddling around with Magento's core source code. An example would be the event 'catalog_product_save_after' which will be raised by Magento immediately after a product is saved. ===== Terminology ===== **Event** Event is something that occurs in a certain place during a particular sequence flow. Say once a customer completes an order, the flow sequence would be to i. Save the order details ii. Send e-mail confirmation to customer Events may be seeded before or after each of these flow points to introduce custom logic. **Observer** Observer is the handler of an event. This listens to any event it is attached to and accordingly handles the event. ===== Customization - Event Vs Overriding ===== Simply put, think about overriding existing core logic if you need to completely change or if you need to extend core logic and your new logic is going to be reused elsewhere. Use events if you are fine with existing logic provided by Magento and need to add over the core logic. ===== Example Usage ===== This example tries to use the Event-Observer methodology to introduce a percentage discount for each product. Currently Magento supports special price functionality without a % discount. So we would use this opportunity to customize magento to introduce %discount at a product level. Before starting, the aim is to ensure that the percentage discount is considered for a simple product when a product is displayed. The event is raised in the class Mage_Catalog_Model_Product_Price->getFinalPrice() (__file__: app/code/core/Mage/Catalog/Model/Product/Price.php). The event is raised by the line Mage::dispatchEvent('catalog_product_get_final_price',array('product'=>$product)); The event that we are about to handle is **catalog_product_get_final_price** which is going to help us add logic to consider the percentage discount. ==== Step 1 ==== Create a new attribute '//percent_discount//'. **Attrib Identifier** –percent_discount , **Scope** – Store View , **Catalog I/p** – Text , **Unique Value** – No , **Values Required** – NoInput , **Validation** –Decimal , **Apply to Configurable/All Product Types** - Yes **Use in quick search** – No , **Advanced Search** – No , **Comparable** – No , **Visibile on Frontend** – Yes , **Attribute Label** – % Discount ==== Step 2 ==== Add this new attribute to your attributeset. If your product's attributeset is '//default//', add the new '//percent_discount//' attribute to this attributeset under "prices" attribute group. ==== Step 3 ==== Register a new custom local module under name 'Xyz'. For this create file '//Xyz.xml//' under directory '//app/etc/modules///'. File contents are - local true ==== Step 4 ==== Register the event with its Observer. Create file '//config.xml//' under directory '//app/code/local/Xyz/Catalog/etc///' with contents as - Xyz_Catalog_Model singleton xyzcatalog/price_observer apply_discount_percent ==== Step 5 ==== Creating the Observer. Create the directory structure - app/code/local/Xyz/Catalog/Model/Price/. Place the php code below in a file by name 'Observer.php' in the directory just created. getEvent(); $product = $event->getProduct(); // process percentage discounts only for simple products if($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) { } else { $percentDiscount = $product->getPercentDiscount(); if (is_numeric($percentDiscount)) { $today = floor(time()/86400)*86400; $from = floor(strtotime($product->getSpecialFromDate())/86400)*86400; $to = floor(strtotime($product->getSpecialToDate())/86400)*86400; if ($product->getSpecialFromDate() && $today < $from) { } elseif ($product->getSpecialToDate() && $today > $to) { } else { $price = $product->getPrice(); $finalPriceNow = $product->getData('final_price'); $specialPrice = $price - $price * $percentDiscount / 100; // if special price is negative - negate the discount - this may be a mistake in data if( $specialPrice < 0) $specialPrice = $finalPriceNow; if ($specialPrice < $finalPriceNow) $product->setFinalPrice($specialPrice); // set the product final price } } } return $this; } } ==== Step 6 ==== Set the discount on the product. Navigate to the catalog product on the admin login and edit a product. Set the percentage discount for this product (under prices subtab). ==== Step 7 ==== Navigate to the product details page on the front end and observe that the new discount has taken effect. To be noted here is that, on all other screens where discounted price is required. An example here is the search results screen, where you would need to add this new attribute to the select query search attributes in method //Mage_CatalogSearch_Block_Result->_getProductCollection()// $_productCollection= $_productCollection->addAttributeToSelect('percent_discount'); Event List: {{dispatcheventlist.xls|}}