开发者

magento accesing "SendFriend" module from category page

I'm trying to add the "send to a friend" action to a category page.

In the product view i can see this code: "canEmailToFriend()): ?>

   <p class="email-friend"><a href="<?php echo this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><?php开发者_运维问答 echo $this->__('Email to a Friend') ?></a></p>

        <?php endif; ?>

If I try to add this code to my "list.phtml" (where products grid is displayed) I receive this error: Invalid method Mage_Catalog_Block_Product_List::canEmailToFriend(Array

saying that this methos is not available in this context...

Who can I add the funcionality of "sendtofriend" module to any page I need?

Thanks in advance


This is because the $this->canEmailToFriend() call is a block method belonging to the product page, a class called Mage_Catalog_Block_Product_View. The product listing page uses a block class called Mage_Catalog_Block_Product_List which does not include this code.

The method canEmailToFriend() contains (as defined in app/code/core/Mage/Catalog/Block/Product/View.php) the logic:

$sendToFriendModel = Mage::registry('send_to_friend_model');
return $sendToFriendModel && $sendToFriendModel->canEmailToFriend();

You could embed that directly in your template and then call the helper to output the link if $sendToFriendModel->canEmailToFriend(), but the best way to achieve this would be to move the canEmailToFriend functionality out into a new helper and call it from there.


I founded an alternative solution, just load sendfriend model

$sendToFriendModel = Mage::getModel('sendfriend/sendfriend');

Then use

<?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;?>

Resource from C:/xampp/htdocs/magento/app/code/core/Mage/Sendfriend/controllers/ProductController.php


I think Mage::registry('send_to_friend_model') returns an object of the class Mage_Sendfriend_Model_Sendfriend. The canEmailToFriend() method in Mage_Sendfriend_Model_Sendfriend checks whether the "email to a friend" functionality is enabled:

You can find these two methods in app/code/core/Mage/Sendfriend/Model/Sendfriend.php:

/**
 * Check if user is allowed to email product to a friend
 *
 * @return boolean
 */
public function canEmailToFriend()
{
    return $this->_getHelper()->isEnabled();
}

/**
 * Retrieve Data Helper
 *
 * @return Mage_Sendfriend_Helper_Data
 */
 protected function _getHelper()
 {
     return Mage::helper('sendfriend');
 }

So you could check that yourself in your template like so:

<?php if (Mage::helper('sendfriend')->isEnabled()) : ?>
    <?php // Do stuff ?>
<?php endif ?>    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜