How to add Name to Magento 1 Newsletter - magento

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)

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: save categories with custom attribute to custom database table

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*/
}
}

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

Event code in Magento v1.7 not working

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

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

Resources