Magento Observer use Helper - magento

What I doing wrong?
config.xml
...
<helpers>
<lacpaycs>
<class>OS_LacPayCS_Helper</class>
</lacpaycs>
</helpers>
</global>
<frontend>
<events>
<sales_model_service_quote_submit_before>
<observers>
<lacpaycs>
<type>singleton</type>
<class>OS_LacPayCS_Model_Observer</class>
<method>lacpaycs_payment_send</method>
</lacpaycs>
</observers>
</sales_model_service_quote_submit_before>
</events>
...
then in Observer Code:
public function lacpaycs_payment_send(Varien_Object $observer)
{
...
$helper = Mage::helper('laqpaycs');
and I getting error - magento tries to get helper from Mage/Laqpaycs/Helper/Data.php
How to say magento that it must get helper from OS/LaqPayCS/Helper/Data.php

You have a typo.
Your helper class group is <lacpaycs> but your helper factory argument is laqpaycs. Change your argument to lacpaycs and, provided that you have a class definition for OS_LacPayCS_Helper_Data at OS/LacPayCS/Helper/Data.php`, your class will be instantiated.

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

Magento : controller_action_predispatch is not working

I am trying to create controller_action_predispatch event:
This is my Mbyte/Pushnotification/etc/config.xml file code
<events>
<controller_action_predispatch_mbyte_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>Mbyte_Pushnotification/Observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_mbyte_pushnotification_index_index>
</events>
My controller code : Mbyte/Pushnotification/controllers/IndexController.php file code
class Mbyte_Pushnotification_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
Mage::log('Run',null,'log.log');
}
}
Observer file code:
class Mbyte_Pushnotification_Model_Observer {
public function indexPreDispatch($observer)
{
Mage::log('Check',null,'observer.log');
}}
controller_action_predispatch is not working.
Is there something i am doing wrong?
The event triggered should be
controller_action_predispatch_pushnotification_index_index
instead of
controller_action_predispatch_mbyte_pushnotification_index_index
in case you did not modify the frontend name ("pushnotification") of your controller.
Therefore you have to update your config.xml to
<events>
<controller_action_predispatch_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>Mbyte_Pushnotification_Model_Observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_pushnotification_index_index>
</events>
you could also write
<events>
<controller_action_predispatch_pushnotification_index_index>
<observers>
<Mbyte_Pushnotification>
<class>pushnotification/observer</class>
<method>indexPreDispatch</method>
</Mbyte_Pushnotification>
</observers>
</controller_action_predispatch_pushnotification_index_index>
</events>
if you named your module "pushnotification", which i assume.
According to magento define event depends should be
controller_action_predispatch_frontName_Controller_Youaction
it is define like:
Mage::dispatchEvent('controller_action_predispatch_' . $this->getFullActionName(),
array('controller_action' => $this));

How do I extend CMS Block on save event of 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>

Magento: Creating an event to extend customer creation in magento

So I am simply trying to create a hook into registrations predispatch event. This is what I have so far:
config.xml:
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<class>mymodulename/observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
and the observer:
Model/Observer.php :
public function hookToAccountCreationBefore($observer) {
die('getting here');
}
So I go and do a registration and I see the controller_action_predispatch_customer_account_createpost event getting called in my event logs but its not calling my function.
Please help!
UPDATE:
The answer below worked perfectly for me. However, $observer->getEvent()->getCustomer() is getting NULL for me even though another observer is overriding the same exact event and this works fine. I've tried temporarily commenting out the observer config for the other extension and its still empty. Any ideas?
In your config.xml you could also do
config.xml:
<global>
<models>
<mymodulename>
<class>MyNamespace_MyModuleName_Model</class>
</mymodulename>
</models>
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<type>singleton</type>
<class>mymodulename/observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
</global>
or
<global>
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mymodulename>
<type>singleton</type>
<class>MyNamespace_MyModuleName_Model_Observer</class>
<method>hookToAccountCreationBefore</method>
</mymodulename>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
</global>
Maybe try with following Event
customer_register_success

Why does my Magento observer get stuck in and endless loop?

My observer gets stuck in and endless loop. Why does it happen and how can I fix it?
config.xml:
<?xml version="1.0"?>
<config>
<global>
<models>
<shipmentsave>
<class>Company_Shipmentsave_Model</class>
</shipmentsave>
</models>
</global>
<adminhtml>
<events>
<sales_order_shipment_track_save_after>
<observers>
<shipmentsave>
<type>model</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentTrackSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_track_save_after>
<sales_order_shipment_save_after>
<observers>
<shipmentsave>
<type>model</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_save_after>
</events>
</adminhtml>
<frontend>
<events>
<sales_order_shipment_save_after>
<observers>
<shipmentsave>
<type>singleton</type>
<class>shipmentsave/observer</class>
<method>salesOrderShipmentSaveAfter</method>
</shipmentsave>
</observers>
</sales_order_shipment_save_after>
</events>
</frontend>
</config>
Observer.php:
class Company_Shipmentsave_Model_Observer
{
public function salesOrderShipmentSaveAfter(Varien_Event_Observer $observer)
{
error_log("My observer called ....",0);
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$track = Mage::getModel('sales/order_shipment_track')
->setNumber('1231354564')
->setCarrierCode('localdelivery')
->setTitle('Aramex');
$shipment->addTrack($track);
$shipment->save();
return;
}
}
Your observer waits for a shipment to get saved, then saves a shipment, which causes it to receive another event (yadda yadda ad nauseum). You will need a way to either escape the loop or not have to save a shipment.
Can you move your event onto sales_order_shipment_save_before and then allow the normal shipment saving to take effect, or do you need the shipment to already have been saved to do your part of the logic?
If so, change the following line on your observer so that Magento uses it as a singleton:
<type>singleton</type> // changed from model
Then, create a variable in your class to track whether you've recursed yet. If so, then just return.
Alternatively, you could check to see if there are any tracking numbers already on the shipment and only save if there are not (and you add one). That will kill the recursion as well.
Let me know if one of those works for you.
Thanks,
Joe
I used
sales_order_shipment_save_after
and its working for me
Thanks :)

Resources