Once an order is placed in my magento store I have a custom module so that i can save the order number into an external database table.
My custom module setup is:
Custom/ExternalOms/config.xml
So i hook into the sales_order_place_after event
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_ExternalOms>
<version>0.0.1</version>
</Custom_ExternalOms>
</modules>
<global>
<models>
<custom_externaloms>
<class>Custom_ExternalOms_ExternalOms</class>
</custom_externaloms>
</models>
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>Custom_ExternalOms_Model_ExternalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
Custom/ExternalOms/Model/ExternalOms.php
and run my function:
class Custom_ExternalOms_Model_ExternalOms extends Mage_Core_Model_Abstract
{
public function exportToOMS()
{
$_order_number = Mage::getSingleton('checkout/session')->getLastRealOrderId();
// remaining code..
}
}
The code runs once the order has been placed correctly but this:
Mage::getSingleton('checkout/session')->getLastRealOrderId();
Is coming up empty
Replace your event by this code
<events>
<sales_order_place_after>
<observers>
<custom_externaloms>
<type>model</type>
<class>externaloms/externalOms</class>
<method>exportToOMS</method>
</custom_externaloms>
</observers>
</sales_order_place_after>
</events>
ExternalOms file by this
class Custom_Externaloms_Model_ExternalOms {
public function exportToOMS($observer)
{
$observer->getOrder();
// remaining code..
}
}
/* in case $observer->getOrder(); will not work than use
Mage::getSingleton('checkout/session')->getLastOrderId(); for getting last order id and load order
*/
I hope it will work
Related
I added custom attribute to a Category "custom_attribute". If selected I need to save this category name and url to new table in a database that I created "store_custom_categories".
This table has columns: id, name, url
How I do that?
Iva,You can used the magento Event function for that case.....
I am creating an extension that case....
app/code/local/Amit/Autoupdatecat/etc/ config.xml
<?xml version="1.0" ?>
<config>
<modules>
<Amit_Autoupdatecat>
<version>1.0.0</version>
</Amit_Autoupdatecat>
</modules>
<global>
<models>
<autoupdatecat>
<class>Amit_Autoupdatecat_Model</class>
</autoupdatecat>
</models>
</global>
<global>
<events>
<catalog_category_save_commit_after>
<observers>
<autoupdatecatgories>
<type>singleton</type>
<class>autoupdatecat/observer</class>
<method>saveCategorytabs</method>
</autoupdatecatgories>
</observers>
</catalog_category_save_commit_after>
</events>
</global>
</config>
app/code/local/Amit/Autoupdatecat/Model/ Observer.php
<?php
class Amit_Autoupdatecat_Model_Observer
{
public function saveCategorytabs($observer)
{
/*get category value */
$data= $observer->getEvent()->getData();
$name=$data['name'];
/*more fields and write code in below for custom table*/
}
}
I have this XML structure in my Alchemy Catalog Module:
<?xml version="1.0" encoding="UTF-8"?>
<!--
#filepath /app/code/local/Alchemy/Catalog/etc
The XML has been extended following
Magento Events API Observers
http://www.excellencemagentoblog.com/magento-part11-series-eventsapi
or
http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/
-->
<config>
<modules>
<Alchemy_Catalog>
<version>0.1.0</version>
</Alchemy_Catalog>
</modules>
<global>
<models>
<alchemycatalog>
<rewrite>
<product>Alchemy_Catalog_Model_Product</product>
<block>Alchemy_Catalog_Model_Block</block>
</rewrite>
</alchemycatalog>
</models>
<events>
<!--
Examples: catalog_product_save_before, catalog_product_prepare_save
Check out Magento events cheat sheet at http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ -->
<catalog_product_save_after>
<observers>
<Alchemy_Catalog>
<type>singleton</type>
<class>Alchemy_Catalog_Model_Product</class>
<method>pingBaseProductService</method>
</Alchemy_Catalog>
</observers>
</catalog_product_save_after>
<catalog_category_save_after>
<observers>
<Alchemy_Catalog>
<type>singleton</type>
<class>Alchemy_Catalog_Model_Product</class>
<method>pingBaseCategoryService</method>
</Alchemy_Catalog>
</observers>
</catalog_category_save_after>
<cms_block_save_before>
<observers>
<Alchemy_Catalog>
<type>singleton</type>
<class>Alchemy_Catalog_Model_Block</class>
<method>rabbitmqBlockProducer</method>
</Alchemy_Catalog>
</observers>
</cms_block_save_before>
</events>
</global>
</config>
and this model that should log a message to prove it works:
<?php
class Alchemy_Catalog_Model_Block extends Mage_Cms_Model_Block {
/**
* Implement function rabbitmqBlockProducer().
* This function writes a message to the rabbit
* mq server
*/
protected $_eventPrefix = 'cms_block';
public function rabbitmqBlockProducer ($event) {
Mage::log('save3 block invoked', null, 'marian.log');
}
/**
* Prevent blocks recursion
*
* #throws Mage_Core_Exception
* #return Mage_Core_Model_Abstract
*/
protected function _beforeSave() {
Mage::log('save2 block invoked', null, 'marian.log');
}
}
But the rabbitmqBlockProducer() method never gets called.
Note: The other methods for product and catalog work just fine.
Any help fixing this or any other method will be appreciated
Mage_Cms_Model_Block does not override the _eventPrefix property, so it therefore will only trigger two generic events before saving: model_save_before and core_abstract_save_before.
Whereas you are already rewriting the CMS block model, you can simply override the _eventPrefix property and set it to cms_block; this will allow your observer to work. However, because you are already performing a rewrite and using your model as an observer, you could simply override the _beforeSave() method and add your logic in there and simply things a bit.
Edit: Your rewrite isn't working. Rewrite syntax for cms/block model is
<global>
<models>
<cms>
<rewrite>
<block>Alchemy_Catalog_Model_Block</block>
</rewrite>
</cms>
</models>
</global>
I'm working on an extension that will receive CatalogEvent information from Magento (Enterprise) when saved and do custom processing on the information.
Here's the code I use to listen to the event:
<?xml version="1.0"?>
<config>
<global>
<models>
<mage4ucustomredirect>
<class>Mage4u_Customredirect</class>
</mage4ucustomredirect>
</models>
<events>
<enterprise_catalogevent_event>
<observers>
<abc>
<type>singleton</type>
<class>Mage4u_Customredirect_Model_Observer</class>
<method>on_enterprise_catalogevent_event</method>
</abc>
</observers>
</enterprise_catalogevent_event>
</events>
</global>
</config>
and this is the observer:
class Mage4u_Customredirect_Model_Observer
{
public function on_enterprise_catalogevent_event(Varien_Event_Observer $observer)
{
Mage::log( "on_enterprise_catalogevent_event" );
}
}
?>
When I save the CatalogEvent, I do not receive the call. Can you spot any problems with my code?
Your observer isn't called, because enterprise_catalog_event is a model, but not an event.
See app/code/core/Enterprise/CatalogEvent/etc/config.xml:
<config>
:
<global>
<models>
<enterprise_catalogevent>
<class>Enterprise_CatalogEvent_Model</class>
<resourceModel>enterprise_catalogevent_resource</resourceModel>
</enterprise_catalogevent>
<enterprise_catalogevent_resource>
:
<entities>
<event>
<table>enterprise_catalogevent_event</table>
</event>
:
</entities>
</enterprise_catalogevent_resource>
</models>
:
</global>
:
</config>
Afaik enterprise_catalogevent/event does not dispatch its own save events, but you could observe the generic model_save_before or model_save_after event, to receive callbacks whenever such model is being created/saved.
You just need to identify the object being passed to these generic events first:
$oObject = $observer->getEvent()->getObject();
if ($oObject instanceof Enterprise_CatalogEvent_Model_Event) {
if ($oObject->isObjectNew()) {
// it is/was a create
else {
// it is/was a save
}
}
Please try this:
<events>
<enterprise_catalogevent_event>
<observers>
<mage4u_customredirect_model_observer>
<type>singleton</type>
<class>Mage4u_Customredirect_Model_Observer</class>
<method>on_enterprise_catalogevent_event</method>
</mage4u_customredirect_model_observer>
</observers>
</enterprise_catalogevent_event>
</events>
I am trying to add Observer to the Product status update event on backend. But it does not triggering the event.
<?xml version="1.0"?>
<config>
<modules>
<Mage4u_Customredirect>
<version>0.1.0</version>
</Mage4u_Customredirect>
</modules>
<global>
<events>
<catalog_product_status_update>
<observers>
<Mage4u_Customredirect_Catalog_product>
<type>singleton</type>
<class>mage4u_customredirect/observer</class> <method>on_catalog_product_status_update</method> </Mage4u_Customredirect_Catalog_product>
</observers>
</catalog_product_status_update>
</events>
</global>
</config>
And the observer function is to receive the status of the product that was updated and based on that i need to update it in another server.
<?php
class Mage4u_Customredirect_Model_Observer
{
public function on_catalog_product_status_update(Varien_Event_Observer $observer)
{
Mage::log("test " ,null,"test");
var_dump($observer);die();
}
}
?>
But it does not triggering this event. Please can someone tel me why its not working.
You should use the same notation here for class name as you use in Mage::getModel() factory method, e.g.: <class>mage4u_customredirect/observer</class>. Do not forget delete cache after this change to make it work.
I would like to set the default number of displayed rows in the admin to higher than 20.
I was following along at http://inchoo.net/ecommerce/magento/magento-admin-grid-how-to-change-number-of-rows/, but I'm trying to make a module to do the task. Magento version is 1.4.2.0.
The error I am getting is Mage registry key "_singleton/grid/observer" already exists.
I have in app/code/local/Company/Custom/etc/config.xml:
<config>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<grid_observer>
<class>grid/observer</class>
<method>applyLimitToGrid</method>
</grid_observer>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
</config>
and in app/code/local/Company/Custom/Model/Observer.php:
class Company_Custom_Grid_Model_Observer
{
public function applyLimitToGrid(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (($block instanceof Mage_Adminhtml_Block_Widget_Grid) && !($block instanceof Mage_Adminhtml_Block_Dashboard_Grid)) {
$block->setDefaultLimit(200);
}
}
}
app/etc/modules/Company_Custom.xml:
<config>
<modules>
<Company_Custom>
<codePool>local</codePool>
<active>true</active>
</Company_Custom>
</modules>
</config>
class Company_Custom_Grid_Model_Observer
should be
class Company_Custom_Model_Observer
You don't have module models class declaration:
<global>
<models>
<modulename>
<class>Namespace_ModuleName_Model</class>
</modulename>
</models>
</global>
The biggest change was in Company/Custom/etc/config.xml, where I put the <events> section inside a <global> block instead of <adminhtml>.
Also inside that file, and fixing the error message was <class>Company_Custom_Model_Observer</class> (where I had grid/observer before...)