Magento - no event for newsletter subscribe & unsubscribe - magento

Why are there no events dispatched on or around the newsletter subscription/un-subscription process either in the customer or newsletter modules.
The only alternative i am faced with at the moment is to use a rewrite for the subscriber model to fit some code in around here.
Does anyone else have a good alternative to this - or am I missing something

I encountered a situation where I needed to listen for subscription/unsubscription events. I encountered this question and thought I would leave this solution here for anyone that may need it:
By adding an observer to the event newsletter_subscriber_save_before in your config.xml:
<global>
....
<events>
....
<newsletter_subscriber_save_before>
<observers>
<your_unique_event_name>
<class>yourgroupname/observer</class>
<method>newsletterSubscriberChange</method>
</your_unique_event_name>
</observers>
</newsletter_subscriber_save_before>
</events>
</global>
You can then call getSubscriber() (in the context of $observer->getEvent(), see next code block) in your observer to get the Mage_Newsletter_Model_Subscriber model that allows you get data about the subscriber. This works for occurrences of subscription and unsubscriptions.
public function newsletterSubscriberChange(Varien_Event_Observer $observer) {
$subscriber = $observer->getEvent()->getSubscriber();
//now do whatever you want to do with the $subscriber
//for example
if($subscriber->isSubscribed()) {
//...
}
//or
if($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED) {
//...
} elseif($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) {
//..
}
}
So it turns out this is really easy. These model events are super powerful and let you do things like this super easily. Can't turn down free functionality!
For quick reference, here is what data the Mage_Newsletter_Model_Subscriber model provides (1.7)

Here's what just worked for me on 1.7.0.2. I know this thread is old, but posting it here in case it can help anyone (since there's not a lot of info about this event out there):
*NOTE: Replace myco_myextension with your extension's unique name:*
In config.xml:
<newsletter_subscriber_save_commit_after>
<observers>
<myco_myextension_model_observer>
<class>Myco_Myextension_Model_Observer</class>
<method>subscribedToNewsletter</method>
</myco_myextension_model_observer>
</observers>
</newsletter_subscriber_save_commit_after>
In Observer.php:
public function subscribedToNewsletter(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$subscriber = $event->getDataObject();
$data = $subscriber->getData();
$statusChange = $subscriber->getIsStatusChanged();
// Trigger if user is now subscribed and there has been a status change:
if ($data['subscriber_status'] == "1" && $statusChange == true) {
// Insert your code here
}
return $observer;
}

The newsletter/subscriber model is a normal Magento model from the looks of it, so it should still dispatch some events from the upstream classes. Take a look at something like newsletter_subscriber_create_after and newsletter_subscriber_delete_after for some possible event hooks to use.

the newsletter modul hooks to event: customer_save_after

Related

Getting product quantity before and after saving a product in magento

I have catch an event catalog_product_save_commit_after and written an observer for it to get product stock.
In config.xml I have written the code below
<events>
<catalog_product_save_commit_after>
<observers>
<Arkix_Logs_observer>
<type>singleton</type>
<class>Arkix_Logs_Model_Observer</class>
<method>get_update_inventory</method>
</Arkix_Logs_observer>
</observers>
</catalog_product_save_commit_after>
</events>
In Observer.php I have written:
public function get_update_inventory( $observer){
$product = $observer->getEvent()->getItem();
$origStockData = $product->getOrigData('stock_item')->getOrigData();//to get original stock
echo '<pre>';print_r($origStockData);
$stockData = $product->getStockItem()->getData();//to get new stock
echo "product name id ".$product->getId();
die('haha');
}
But error is coming Call to a member function getOrigData() on non object in observer.php
I think you need to instantiate the model Mage::getModel('cataloginventory/stock_item') before using the getOridData() function. So
try,
public function get_update_inventory( $observer){
$productId = $observer->getProduct()->getId();
$model = Mage::getModel('catalog/product');
$_product = $model->load($productId);
$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($_product)->getQty();
print_r($stocklevel);
}
comment here if you have any doubt.
For anybody that comes across this, there are 2 things wrong here:
...
<catalog_product_save_commit_after>
...
</catalog_product_save_commit_after>
The event that you should be using is
...
<catalog_product_save_after>
...
</catalog_product_save_after>
Since the method getOridData() is only available before the transaction is commited.
The method to get the observer product is $observer->getEvent()->getProduct(); , not $observer->getEvent()->getItem();
May be you can try following code instead of using getOrigData()
$product->getStockItem()->getQty();

Observer runs only once

Event only runs once. I am adding custom value in quote after product add in cart through observer on event checkout_quote_init
it works fine but if customer is logged in and place order then in first order value is inserted well in quote but in second time observer is not runing.
<checkout_quote_init>
<observers>
<youva_custom>
<type>singleton</type>
<class>custom/observer</class>
<method>setqoutevalue</method>
</youva_custom>
</observers>
</checkout_quote_init>
public function setqoutevalue($observer) {
$event = $observer->getEvent();
$_quote = $event->getQuote();
$_quote->setCustomevalue($customevalue);
$_quote->save();
}
If you take a look at the code that dispatches the event you are using you will see this (Mage_Checkout_Model_Session::getQuote()):
if (!$this->getQuoteId()) {
if ($customerSession->isLoggedIn() || $this->_customer) {
$customer = ($this->_customer) ? $this->_customer : $customerSession->getCustomer();
$quote->loadByCustomer($customer);
$this->setQuoteId($quote->getId());
} else {
$quote->setIsCheckoutCart(true);
Mage::dispatchEvent('checkout_quote_init', array('quote'=>$quote));
}
}
This means that the event is dispatched only when the quote is created. and only when the user is not logged in. This means that if there is already a quote id in the session the event won't get dispatched.
You may want to use a different event. For example checkout_cart_product_add_after

magento disable proceed to checkout until condition is true

What magento method I can use to stop the checkout button to function until a certain condition its true?
basically is a user has deleted a product on basket while he must not checkout until two products are on the basket
public function deleteAction()
{
parent::deleteAction();
if($this->_getCart()->getQuote()->getItemsCount() == 1) {
$this->addErrorMessage('Please remove one voucher. Or add one More infant milk product');
}
}
This is quite easy to accomplish using the config flag checkout/options/onepage_checkout_enabled:
public function deleteAction()
{
parent::deleteAction();
if($this->_getCart()->getQuote()->getItemsCount() == 1) {
Mage::app()->getStore()->setConfig('checkout/options/onepage_checkout_enabled',0);
}
}
A side note:
It's not really necessary to rewrite the controller here, as you could handle it in a postdispatch controller observer action:
<events>
<controller_action_postdispatch_checkout_cart_delete>
<observers>
<yourmodule_postdispatch_delete>
<class>yourmodel/observer</class>
<method>deletePostdispatch</method>
</yourmodule_postdispatch_delete>
</observers>
</controller_action_postdispatch_checkout_cart_delete>
</events>
And the observer method would look like:
public function deletePostdispatch($observer)
{
if(Mage::getSingleton('checkout/session')->getQuote()->getItemsCount()==1){
Mage::app()->getStore()->setConfig('checkout/options/onepage_checkout_enabled',0);
}
}
HTH, Cheers!

Redirecting to checkout after account is created and products are in cart

I have a special case where users have to sign up for an account via the account sign up page (e.g. /customer/account/create/). Upon completion, and in the event that they have a product in the cart, I need to redirect them back to the checkout screen.
I currently have an observer in place that listens to the customer_register_success event. The observer upgrades the users account to a membership group via this code:
class Hatclub_MembershipHandler_Model_Observer {
// members group id
const GROUP_ID = 4;
// called when a customer registers for the site
public function registrationSuccess(Varien_Event_Observer $observer) {
// extract customer data from event
$customer = $observer->getCustomer();
// a cookie should have been set with the membership id
if (isset($_COOKIE['membership_account_id'])) {
$customer
->setGroupId(self::GROUP_ID)
->setRmsId($_COOKIE['membership_account_id']);
}
return $this;
}
}
Is there another event that I can listen to that is better suited for what I want to do? I have a redirect_to cookie that is available as well if that helps.
After much trial and error, I came across a solution. Since I'm already using the customer_register_success event and modifying user data with my observer, I had to use another event called customer_save_after and it worked like a charm.
config.xml event block
<customer_save_after>
<observers>
<customer_session_observer>
<class>hatclub_membership_handler/observer</class>
<method>customerSave</method>
<type>singleton</type>
</customer_session_observer>
</observers>
</customer_save_after>
observer.php method
public function customerSave() {
// set redirect url from cookie, default to null
$redirect_url = (isset($_COOKIE['redirect_url']))
? isset($_COOKIE['redirect_url']) : null;
// if a redirect url was specified
if (isset($redirect_url)) {
// remove cookie to prevent infinite loop
unset($_COOKIE['redirect_url']);
// redirect to provided url
Mage::app()->getResponse()
->setHeader('Location', $redirect_url)
->sendHeaders();
}
}
The redirect within an observer was a bit of a struggle, but I managed to accomplish it using
Mage::app()->getResponse()
->setHeader('Location', $redirect_url)
->sendHeaders();
Suggestions and criticism welcome.

Magento Observer generate errors and Magento areas confusing

I've made controller_front_init_routers event observer, which retrieves data from a REST service to construct a menu.
All was ok until I discovered that the observer generates errors in the backend (no way to save products for example) and also in the rest services.
I am struggling for any conclusions, So I raised some interrogations.
I tried to make a condition in order to trigger my Observer
methods in case we are in frontend only. But Magento considers that we are
always in frontend area.
(var_dump(Mage::app()->getStore()->isAdmin()) return always false
and the same with var_dump(Mage::getDesign()->getArea() ==
'adminhtml'))
Can anyone explain what's happened ?
Also one solution is to place the event observer in frontend
area in the config.xml and to load it with
Mage::app()->loadArea($this->getLayout()->getArea()); but where should I
place this piece of code ? in a new observer ? Is that the most
appropriate process ?
Is it a way to listen once an event then to pause the listener?
(once my menu is registered, I don't need to listen to the event any more)
Is the use of controller_front_init_routers event the best choice ?
Who has ever seen that kind of problem ?
I work on Magento ver. 1.12.0.2
Here the config.xml
<globals>
....
<events>
<controller_front_init_routers>
<observers>
<connector_services_observer>
<type>singleton</type>
<class>Connector_Services_Model_Observer</class>
<method>getEvent</method>
</connector_services_observer>
</observers>
</controller_front_init_routers>
</events>
</globals>
Here the function getEvent in my model observer
public function getEvent($observer){
//model which do post or get requests and return xml and menu
$_getRest = Mage::getModel('connector_services/RestRequest');
//the paths
$_menu_url = Mage::getStoreConfig('connector_service_section/connector_service_url/service_menu_url');
//put a store config
$path_nlist = 'veritas-pages-list.xml';
$_isAdmin = Mage::helper('connector_services');
$currentUrl=Mage::helper("core/url")->getCurrentUrl();
//the way I found to trigger methods only in frontend
//that's not very beautiful I know
$admin = preg_match("#/admin/#",$currentUrl);
$api = preg_match("#/api/#",$currentUrl);
//
if ( !$admin && ! $api ){
$_menuXml = $_getRest->postRequest($_menu_url);
if( $_menuXml )
{
$_menu = $_getRest->makeMenu($_menuXml);
Mage::register('menu',$_menu);
}
}
You should be able to pass a querystring to the rest service, similar to how you'd just type it out in the address bar. Magento will forward it on to the observer, and you can use it as a flag.
Add something like the following to your code:
const USE_FRONTEND = 'usefront';
public function getEvent($observer){
this->request = $observer->getEvent()->getData('front')->getRequest();
// If the constant is in the query string
if ($this->request->{self::USE_FRONTEND}) {
// Do code related to this request
die('Frontend flag detected');
}
}
Call to your site like this and pass the querystring
http://www.yourmagentosite.com/?usefront=true
I am not extremely familiar with Magento's new REST api, but I know it works in the browser. Maybe this explanation can help you out.

Resources