How do I extend CMS Block on save event of Magento? - magento

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>

Related

Magento - Getting 'getLastRealOrderId' once order has been placed

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

Can not override Sales Resouce Model Collection class

I am trying to override Mage_Sales_Model_Resource_Order_Collection
My custom module's config:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Abc_Salesextend>
<version>0.1.0</version>
</Abc_Salesextend>
</modules>
<global>
<blocks>
<salesextend>
<class>Abc_Salesextend_Block</class>
</salesextend>
<adminhtml>
<rewrite>
<sales_order_grid>Abc_Salesextend_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<models>
<salesextend>
<class>Abc_Salesextend_Model</class>
<resourceModel>salesextend_resource</resourceModel>
</salesextend>
<salesextend_resource>
<class>Abc_Salesextend_Model_Resource</class>
</salesextend_resource>
<!-- HERE is i am trying to override-->
<sales_resource>
<rewrite>
<order_collection>Abc_Salesextend_Model_Resource_Order_Collection</order_collection>
</rewrite>
</sales_resource>
</models>
</global>
</config>
It's not giving me any kind of error even if I place the wrong custom class name. So it's not finding my custom class.
Please help
I am not quite sure why the rewrite fails. The rewrite definition looks good. The error can only be related to the wrong class used for rewriting, a cache problem (config.xml cache) or to your module not being active.
I guess you want to just manipulate the collection so instead of a rewrite you should use some event/observer technique.
/**
*
* Sales order grid collection
* #param unknown_type $observer
*/
public function salesOrderGridCollectionLoadBefore($observer){
$collection = $observer->getOrderGridCollection();
$select = $collection->getSelect();
$select->DO_WHATEVER_YOU_WANT...
}
and this method is triggered by:
<events>
<sales_order_grid_collection_load_before>
<observers>
<cartware_modify_grid_adminhtml_collection>
<model>your_module/observer_block</model>
<method>salesOrderGridCollectionLoadBefore</method>
</cartware_modify_grid_adminhtml_collection>
</observers>
</sales_order_grid_collection_load_before>
</events>
</adminhtml>
Good luck!
<sales>
<rewrite>
<resource_order_collection>Abc_Salesextend_Model_YOURFILENAME</resource_order_collection>
</rewrite>
</sales>
just replace with you file name in YOURFILENAME,,,it works...

Magento trigger observer with custom orderstatus

I'm creating a Magento module and I want the Observer to trigger when my custom orderstatus is chosen.
Practical situation:
People select: 'Payment Received' the //DO STUFF in the Observer is triggered.
This ain't working, so what is wrong here? (the status shows up correctly though) :-(
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MOD_PaidStatus>
<version>1.0.0</version>
</MOD_PaidStatus>
</modules>
<global>
<sales>
<order>
<statuses>
<payment_received translate="label"><label>Payment Received</label></payment_received>
</statuses>
<states>
<processing translate="label">
<label>Processing</label>
<statuses>
<processing default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</processing>
<pending translate="label">
<label>Pending</label>
<statuses>
<pending default="1"/>
<payment_received default="2" />
</statuses>
<visible_on_front/>
</pending>
</states>
</order>
</sales>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<PaidStatus>
<class>MOD_PaidStatus/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</PaidStatus>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
</config>
And my Observer.php looks like:
class MOD_PaidStatus_Model_Observer {
public function observeAttributeChange($observer){
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
mail('test#testcase.com', 'testcase', 'foo bar?');
}
}
}
In order to be able to execute some actions on changing the order's attribute "status" it is preferable to observe an event. In this case the event would be sales_order_resource_save_attribute_after. The observer function could be set up as follows:
class YourPackage_YourModule_Model_Observer {
public function observeAttributeChange($observer)
{
$attribute = $observer->getEvent()->getAttribute();
if ($attribute->getAttributeCode() == 'status') {
// DO STUFF
}
}
In your config.xml enter the necessary observer configuration
<global>
<events>
<sales_order_resource_save_attribute_after>
<observers>
<your_observer_node_name>
<class>yourpackage_yourmodule/observer</class>
<method>observeAttributeChange</method>
<type>singleton</type>
</your_observer_node_name>
</observers>
</sales_order_resource_save_attribute_after>
</events>
</global>
You can do this with rewriting or observer.
You do a kind of mix of these 2 concepts.
To use rewrite, do not name your file Observer, use something like Order.php :
class MOD_PaidStatus_Model_**Order** extends Mage_Sales_Model_Order
and in your config.xml, in the global node, add this :
<models>
<sales>
<rewrite>
<order>MOD_PaidStatus_Model_Order</order>
</rewrite>
</sales>
</models>
If you want to go through an Observer, use the name Observer.php and the classname should be :
MOD_PaidStatus_Model_Observer
and it shouldn't extends anything.
The function is more like
public function myEventHandling($event)
{
$order = $event->getOrder();
$status = $order->getStatus();
//DO YOUR STUFF here
}
But your should listen for a order_status_changed_after event that doesn't exist, so you add to trigger it too... Very big stuff if you're not a confirmed developer.
If you want something like this, you need an event node in your config.xml file to associate your Observer and its method to the event. Make some search about event handling in Magento
Regards,
Edit.: for observer and event way, look the previous post. But handling such a generic event is a very resource consuming way.

Magento Enterprise CatalogEvent

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>

Observer for Magent product after save

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.

Resources