Here I am going show how to create a simple product attribute programatically.I have created a simple script which is creating the product attribute when I do install my module or upgrade my module.So please look at the below code which I have created.
Here you will be getting how you can use event observer before and after any events in Magento. Here, you can add your custom event before and after any event raised by Magento’s core. In this way, we don’t have to use the dispatched events. This way of adding custom event before and after save is useful when we just need to add or do something before or after any Magento core event. For example, setting some values to some attributes before or after product is saved.
For this, you just need to add some event observer code in your module’s MyModule/etc/config.xml file and add a new observer MyModule/Model/Observer.php
In the example code below, I have added before event hook for customer address. Catalog product hooks run after saving products in admin.
customer_address_save_before = runs just before saving customer address customer_address_save_after = runs immediately after saving customer address
catalog_product_save_before = runs just before saving product catalog_product_save_after = runs just after saving product
Similarly, you can add your custom function to other events like sales_order_save_before, sales_order_save_after, sales_order_invoice_save_before, sales_order_invoice_save_after, etc.
Config.xml :
Observer.php :
Here you will be getting how you can use event observer before and after any events in Magento. Here, you can add your custom event before and after any event raised by Magento’s core. In this way, we don’t have to use the dispatched events. This way of adding custom event before and after save is useful when we just need to add or do something before or after any Magento core event. For example, setting some values to some attributes before or after product is saved.
For this, you just need to add some event observer code in your module’s MyModule/etc/config.xml file and add a new observer MyModule/Model/Observer.php
In the example code below, I have added before event hook for customer address. Catalog product hooks run after saving products in admin.
customer_address_save_before = runs just before saving customer address customer_address_save_after = runs immediately after saving customer address
catalog_product_save_before = runs just before saving product catalog_product_save_after = runs just after saving product
Similarly, you can add your custom function to other events like sales_order_save_before, sales_order_save_after, sales_order_invoice_save_before, sales_order_invoice_save_after, etc.
Config.xml :
singleton mymodule/observer catalog_product_save_after singleton mymodule/observer customer_address_save_before
Observer.php :
class MyNamespace_MyModule_Model_Observer { public function customer_address_save_before($observer) { $address = $observer->getCustomerAddress(); // do something here } public function catalog_product_save_after($observer) { $product = $observer->getProduct(); /*here for exapmle I would like to set product name in meta title text box automatically if I save product in admin side*/ if(!$product->getMetaTitle()){ $name = $product->getName(); $metaTitle = str_replace(' - ', ' ', $name); $product->setMetaTitle($metaTitle); $product->getResource()->saveAttribute($product, 'meta_title'); } } }
No comments:
Post a Comment