For all the features and benefits with using Magento Enterprise, there will always be some quirks where you may think to yourself; ‘this doesn't work how it should’.
Recently during our quality assurance step for a client's website, we came across a minor bug with Magento EE rewards points. Users who have subscribed to the newsletter would still visually see on the frontend 'Subscribe to our newsletter now and earn x Reward points'. Nothing groundbreaking, but a small gripe none the less.
If you take a look at the enterprise layout reward.xml you will see this message is defined here. Unfortunately we cannot remove it based on whether the user has already subscribed once.
rewards.xml for newsletter
enterprise_reward/action_newsletter reward-newsletter Subscribe to our newsletter now and earn %s.
tooltip.phtml template
In rewards.xml you can see that most, if not all rewards messages use this template to render the content.
<code class="language-php"><?php if ($this->hasRewardMessage()):?> <?php echo $this->__($this->getRewardMessage(), Mage::helper('enterprise_reward')->formatReward($this->getRewardPoints(), $this->getRewardAmount(), null, '%s'))?> <?php endif;?></code>
Solution
Originally I had created a new action method assigning a helper method to check for the subscribed email existing and adding this check by overriding the _prepareTemplateData() method for the Enterprise_Reward_Block_Tooltip class. If the action method was defined and returning 1, it would change all reward messages to blank.
Although not a bad idea, as you would be able to hide any rewards message based on some conditional check, I changed it to overriding the tooltip.phtml template instead, adding the subscribe check because of the pure fact I was editing a core method. Ben Marks would probably call the Magento police if I left it as is.
What we need to do first is create a helper method to test if the customer has ever subscribed to the newsletter. If the user has subscribed, you will be able to see their email in the database table 'newsletter_subscriber'.
Create a folder structure and file like the one listed below:
- app
- code
- local
- Cti
- RewardExtended
- etc
- config.xml
- Helper
- Data.php
In the helper Data.php file copy and paste the following:
<code><?php class Cti_RewardExtended_Helper_Data extends Mage_Core_Helper_Data { // add a helper method for toggling newsletter message is user has subscribed once public function isSubscribed() { $_email = Mage::getSingleton('customer/session')->getCustomer()->getData('email'); $_subscribedEmail = Mage::getModel('newsletter/subscriber')->loadByEmail($_email, 'subscriber_email'); if( $_subscribedEmail->getId() && !empty($_email)) { $isSubscribed = 1; return $isSubscribed; } } }</code>
What we are doing here is checking if the customer email exists and seeing if that email is in the subscriber list.
In config.xml copy and paste the following:
<code><?xml version="1.0"?> <config> <modules> <Cti_RewardExtended> <version>1.0.0</version> </Cti_RewardExtended> </modules> <global> <helpers> <cti_rewardextended> <class>Cti_RewardExtended_Helper</class> </cti_rewardextended> </helpers> </global> </config></code>
Now in your theme folder add a directory in template/reward called 'tooltip-newsletter.phtml'.
The path I will be adding to is 'app/design/frontend/cti/theme/rewards/tooltip-newsletter.phtml’.
Copy and paste the following code in this file:
<code><?php /** * @see Enterprise_Reward_Block_Tooltip */ $_subscribed = Mage::helper('cti_rewardextended/data')->isSubscribed(); ?> <div class="reward-message <?php echo $this->getWrapperClass()?>"> <div class="reward"> <?php if ($this->hasRewardMessage()):?> <?php if(empty($_subscribed)):?> <p><?php echo $this->__($this->getRewardMessage(), Mage::helper('enterprise_reward')->formatReward($this->getRewardPoints(), $this->getRewardAmount(), null, '<strong>%s</strong>'))?></p> <?php else:?> <p><?php echo $this->__($this->getRewardAmount(), null, '<strong>%s</strong>')?></p> <?php endif;?> <?php endif;?> <?php if ($this->hasPointsBalance() && !$this->hasHideBalance()):?> <p class="balance"> <?php echo $this->__('Your current balance is %s.', Mage::helper('enterprise_reward')->formatReward($this->getPointsBalance(), null, null, '<strong>%s</strong>'))?> </p> <?php endif;?> <?php if (!$this->hasHideLink()):?> <p class="a-more"><?php echo $this->renderLearnMoreLink()?></p> <?php endif;?> <?php if ($this->hasQtyLimit() && $this->hasRewardQtyLimitationMessage()):?> <p class="limit"><?php echo $this->__($this->getRewardQtyLimitationMessage(), "<strong>{$this->getQtyLimit()}</strong>")?></p> <?php endif;?> <?php if ($this->hasGuestNote()):?> <p class="limit"><?php echo $this->__($this->getGuestNote())?></p> <?php endif;?> </div> </div></code>
Look between line 9 to 15 to see where the magic happens. With this new helper method, we check to see if $_subscribed is equal to something and if it is, remove the reward message.
All we need to do now, is add a 'setTemplate' call to your local.xml theme file or layout module. For simplicity we will add it in local.xml. Copy and paste the following in your theme local.xml file:
<code><newsletter_manage_index> <reference name="reward.tooltip.newsletter"> <action method="setTemplate"> <template>reward/tooltip-newsletter.phtml</template> </action> </reference> </newsletter_manage_index></code>
Now sit back, refresh the page and see that the reward points for subscribers is now gone for customers who have already signed up once.
I hope you have found this post useful and please feel free to comment.
Note: This post provides a temporary fix until Magento looks at addressing this issue.