‘Email a Friend’ link outwith product view in Magento

How to add an ‘Email a Friend’ link to the footer.phtml template in Magento. This is well worth a blog post – for the simple reason that despite searching extensively on Google, I came up with very little. One of those areas where the results for searches like ‘Magento email friend in cms page’ and such like just threw up loads of results relating to people not able to get Magento to send out any emails at all.

First of all the code that would normally be used in the product view.phtml template to allow the user to send a link to the product via email would be;


<?php if ($this->canEmailToFriend()): ?>
     <p class="email-friend"><a href="<?php echo $this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><span><span><?php echo $this->__('Email to a Friend') ?></span></span></a></p>
<?php endif; ?>

My task in this case was to follow the designer’s designs and place this link in the footer rather than directly beneath the ‘Add to Cart’ functionality in the product view. There are two issues with this;

  • The first relates to this answer by Nick on Stackoverflow. Basically the $this->canEmailToFriend() call is a block method belonging to the product page, a class called Mage_Catalog_Block_Product_View. This class isn’t accessed in the footer block so you have no way of knowing if the ‘Email a Friend’ functionality has been activated.
  • The view.phtml template accesses the product info using $this (ie getProduct(); ?> ). This won’t work in the footer.

Again using Nick’s answer the first part of the solution is to replicate the canEmailToFriend() method directly in the template. The second part involves accessing the current product directly from the registry and using the same helper function to create the link to the product. So, at the top of footer.phtml;

	
<?php $_helper = $this->helper('catalog/output'); ?>

And where you want to place the link;


<?php $sendToFriendModel = Mage::registry('send_to_friend_model'); ?>
<?php $_product = Mage::registry('current_product'); ?>
						
<?php if ( $sendToFriendModel->canEmailToFriend() ) : ?>
	<p class="email-friend"><a href="<?php echo $this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><span><span><?php echo $this->__('Email to a Friend') ?></span></span></a></p>
<?php endif;?>