Event code in Magento v1.7 not working - events

G'Day all,
I'm trying to write my first magento event handler to capture "checkout_cart_save_after" but I can't get my Observer.php code to fire, after 2 days of looking at web sites on magento events I understand the whole process but the code is not running.. I'm perplexed as to why.
Here is the modules xml file in /var/magento/app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<codePool>local</codePool>
<active>true</active>
<version>1.0.0</version>
</Namespace_Yourmodule>
</modules>
</config>
As you can see I'm just using Namespace and Yourmodule as I'm writing a tutorial for others....
In my module's etc directory (/var/magento/app/code/local/Namespace/Yourmodule/Module) I have config.xml
<?xml version="1.0"?>
<global>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
and in the Modules directory I have my Observer.php file:
class Namespace_Yourmodule_Model_Observer
{
/*----------------------------------------
*
* LogInfo()
*
* Basic logging of activity to disk for debugging
*/
public function LogInfo($msg)
{
$logfile="/logs/Namespace-module.log";
$fd=fopen($logfile,"a");
if($fd)
{
fwrite($fd,$msg."\n");
fclose($fd);
}
}
public function checkout_cart_save_before($observer)
{
LogInfo("save_before called");
}
public function checkout_cart_save_after($observer)
{
LogInfo("save_after called");
}
}
The result of trying to add and remove items from the cart is that no log file is crearted and when manually created, no data is written, system.log shows no errors, if I skew the XML it reports the errror, so the XML file is being read.
Any thoughts on what I have missed???
Sid
Update 12/2:
I have spent the weekend on this and solved the issue, thanks for the hints, it was a combination of minor things that I should have picked up and the format of the XML file now matches what has been suggested above... there is now a tutorial! I have fully documented the process with working code and config files.
See http://z900collector.wordpress.com/magento/magento-events/

Try updating your config.xml to
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<version>1.0</version>
</Namespace_Yourmodule>
</modules>
<global>
<!--helpers>
<yourmodule>
<class>Namespace_Yourmodule_Helper</class>
</yourmodule>
</helpers-->
<models>
<yourmodule>
<class>Namespace_Yourmodule_Model</class>
</yourmodule>
</models>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
</config>
See Customize Magento using Event/Observer

Related

How to add Name to Magento 1 Newsletter

I've been trying to add the subscriber first name to the Magento newsletter. I've can't find anything on Google. I've looked at Adding a custom field to Magento's subscription module
but it's not clear for me. I have the field in newsletter_subscriber table called subscriber_firstname in the database and admin grid.
I created a module like this:
In app/etc/modules/Afe_Newslettername.xml
<?xml version="1.0"?>
<config>
<modules>
<Afe_Newslettername>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Newsletter/>
</depends>
</Afe_Newslettername>
</modules>
</config>
In app/code/local/Afe/Newslettername/etc/config.xml
<?xml version="1.0"?>
<config>
<global>
<frontend>
<events>
<newsletter_subscriber_save_before>
<observers>
<afe_newslettername>
<class>afe_newslettername/observer</class>
<method>newsletterSubscriberSave</method>
</afe_newslettername>
</observers>
</newsletter_subscriber_save_before>
</events>
</frontend>
</global>
</config>
In app/code/local/Afe/Newslettername/Model/Observer.php
<?php
class Afe_Newslettername_Model_Observer
{
public function newsletterSubscriberSave(Varien_Event_Observer $observer)
{
$subscriber = $observer->getEvent()->getSubscriber();
$name = Mage::app()->getRequest()->getParam('subscriber_firstname');
$subscriber->setSubscriberName($name);
return $this;
}
}
...but the name doesn't gets saved to the database or displayed in the admin newsletter subscriber grid.
What am I missing?
UPDATE:
I changed my config.xml to:
<?xml version="1.0"?>
<config>
<modules>
<Afe_Newslettername>
<version>1.0.0.0</version>
</Afe_Newslettername>
</modules>
<global>
<models>
<afenewsletters>
<class>Afe_Newslettername_Model</class>
</afenewsletters>
</models>
</global>
<events>
<newsletter_subscriber_save_before>
<observers>
<add_name>
<class>afe_newslettername/observer</class>
<method>newsletterSubscriberSave</method>
</add_name>
</observers>
</newsletter_subscriber_save_before>
</events>
</config>
But now I get this error:
Warning: include(Mage/Afe/Newslettername/Model/Observer.php): failed to open stream: No such file or directory in /home/www/public_html/lib/Varien/Autoload.php on line 94
Warning: include(): Failed opening 'Mage/Afe/Newslettername/Model/Observer.php' for inclusion (include_path='/home/www/public_html/app/code/local:/home/www/public_html/app/code/community:/home/www/public_html/app/code/core:/home/www/public_html/lib:.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/www/public_html/lib/Varien/Autoload.php on line 94
Not sure why it's looking in the Mage folder?
I think there are some issue in your event declaration code. Please try below code. Hope it works.
config.xml file:
<newsletter_subscriber_save_before>
<observers>
<afe_newslettername>
<class>Afe_Newslettername_Model_Observer</class>
<method>newsletterSubscriberSave</method>
</afe_newslettername>
</observers>
</newsletter_subscriber_save_before>
Observer.php file:
public function newsletterSubscriberSave($observer)

Change Magento default status for duplicated products

I have a Magento store installed, and when a product is duplicated in the backend, Magento sets its status to Disabled by default. I don't want that to happen, the duplicated product should have its status copied from the original product as well.
In this post a partial solution was given. I see where I can find the config.xml and make the necessarry changes. However, where do I put such an observer class? Which file should I use/create and would that require any changes to the config.xml input?
Or does somebody have an overall solution for this issue? Thanks in advance!
Try this:
Create: app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<version>1.0.1</version>
</MagePal_EnableDuplicateProductStatus>
</modules>
<global>
<models>
<enableduplicateproductstatus>
<class>MagePal_EnableDuplicateProductStatus_Model</class>
</enableduplicateproductstatus>
</models>
<events>
<catalog_model_product_duplicate>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>enableduplicateproductstatus/observer</class>
<method>productDuplicate</method>
</enableduplicateproductstatus>
</observers>
</catalog_model_product_duplicate>
</events>
</global>
</config>
Create: app/code/local/MagePal/EnableDuplicateProductStatus/Model/Observer.php
class MagePal_EnableDuplicateProductStatus_Model_Observer
{
/**
* Prepare product for duplicate action.
*
* #param Varien_Event_Observer $observer
* #return object
*/
public function productDuplicate(Varien_Event_Observer $observer)
{
$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
return $this;
}
}
Create: app/etc/modules/MagePal_EnableDuplicateProductStatus.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<active>true</active>
<codePool>local</codePool>
</MagePal_EnableDuplicateProductStatus>
</modules>
</config>
Then clear cache and try duplicating a product.
read more # :
http://magento4u.wordpress.com/2009/06/08/create-new-module-helloworld-in-magento/
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
make a new product active by default in magento
I found error on this code and find out the solution below:
On app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml change
<method> duplicateProduct </method>
TO
<method>productDuplicate</method>

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.

magento change default number rows displayed in adminhtml grid pages

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...)

How do i override the "addAction()" funciton under app\code\core\Mage\Checkout\controllers

I am trying to add some data to a session variable when a product is added to the shopping cart...
I tried copying the "CartController.php" under "app\code\core\Mage\Checkout\controllers" to
"local\Mage\Checkout\controllers" but its not actually overriding the controller..
Could anyone give me some hint on this..?
Thanks a lot.
You can create an "Event Oberserver" to trigger an event.
Create the following folders :
app/code/local/[myMagento]/EventTest/etc
app/code/local/[myMagento]/EventTest/Model
create a file under /EventTest/Model directory like myMagento_EventTest.xml and put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<active>true</active>
<codePool>local</codePool>
</myMagento_EventTest>
</modules>
</config>
create config.xml file under /EventTest/etc folder nad put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<version>0.1.0</version>
</myMagento_EventTest>
</modules>
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<myMagento_EventTest_Model_Observer>
<type>singleton</type>
<class>myMagento_EventTest_Model_Observer</class>
<method>MyTestMethod</method>
</myMagento_EventTest_Model_Observer>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
</config>
create model class file observer.php under the /EventTest/Model directory and put following
<?php
class myMagento_EventTest_Model_Observer {
public function MyTestMethod($observer) {
$event = $observer->getEvent(); //Fetches the current event
$product = $event->getProduct();
$eventmsg = "Current Event Triggered : " . $event->getName() . "
Currently Added Product : " . $product->getName();
//Adds Custom message to shopping cart
echo Mage::getSingleton("checkout/session")->addSuccess($eventmsg);
//Your Custom Logic Here
//you can use print_r($product) here to get more details
}
}
I found this , this enables us to override the CartController.php addAction() funciton... This is exaclty i needed.
Hope this would help someone in need.
Balan

Resources