1

I am trying to override Sendfriend magento core module product controller to local ,But It is not working below is the given workaround .

app/etc/modules/Companyname_Sendfriend.xml

<config>
    <modules>
        <Companyname_Sendfriend>
            <active>true</active>
            <codePool>local</codePool>
        </Companyname_Sendfriend>
    </modules>
</config>

Config.xml file for the overridden local sendfriend module

<?xml version="1.0"?>
<config>
    <modules>
        <Companyname_Sendfriend>
             <version>1.6.0.0</version>
        </Companyname_Sendfriend>
    </modules>
    <frontend>
        <routers>
            <sendfriend>
                <args>
                    <modules>
                        <Companyname_sendfriend before="Mage_Sendfriend_ProductController">
                            Companyname_Sendfriend_Product
                        </Companyname_Sendfriend>
                    </modules>
                </args>
            </sendfriend>
        </routers>
    </frontend>
</config>

At last the ProductController.php code which is to be called

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sendfriend
 * @copyright  Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Email to a Friend Product Controller
 *
 * @category    Mage
 * @package     Mage_Sedfriend
 * @author      Magento Core Team <core@magentocommerce.com>
 */
require_once Mage::getModuleDir('controllers', 'Mage_Sendfriend').DS.'ProductController.php';
class Companyname_Sendfriend_ProductController extends Mage_Sendfriend_ProductController
{
    /**
     * Predispatch: check is enable module
     * If allow only for customer - redirect to login page
     *
     * @return Mage_Sendfriend_ProductController
     */
    public function indexAction()
    {
         echo "called";
        exit;
    }
    public function preDispatch()
    {


        parent::preDispatch();

        /* @var $helper Mage_Sendfriend_Helper_Data */
        $helper = Mage::helper('sendfriend');
        /* @var $session Mage_Customer_Model_Session */
        $session = Mage::getSingleton('customer/session');
        echo "called";
        exit;
        if (!$helper->isEnabled()) {
            $this->norouteAction();
            return $this;
        }

        if (!$helper->isAllowForGuest() && !$session->authenticate($this)) {
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            if ($this->getRequest()->getActionName() == 'sendemail') {
                $session->setBeforeAuthUrl(Mage::getUrl('*/*/send', array(
                    '_current' => true
                )));
                Mage::getSingleton('catalog/session')
                    ->setSendfriendFormData($this->getRequest()->getPost());
            }
        }

        return $this;
    }

    /**
     * Initialize Product Instance
     *
     * @return Mage_Catalog_Model_Product
     */
    protected function _initProduct()
    {
        $productId  = (int)$this->getRequest()->getParam('id');
        if (!$productId) {
            return false;
        }
        $product = Mage::getModel('catalog/product')
            ->load($productId);
        if (!$product->getId() || !$product->isVisibleInCatalog()) {
            return false;
        }

        Mage::register('product', $product);
        return $product;
    }

    /**
     * Initialize send friend model
     *
     * @return Mage_Sendfriend_Model_Sendfriend
     */
    protected function _initSendToFriendModel()
    {
        $model  = Mage::getModel('sendfriend/sendfriend');
        $model->setRemoteAddr(Mage::helper('core/http')->getRemoteAddr(true));
        $model->setCookie(Mage::app()->getCookie());
        $model->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

        Mage::register('send_to_friend_model', $model);

        return $model;
    }

    /**
     * Show Send to a Friend Form
     *
     */
    public function sendAction()
    {
         echo "called";
        exit;

        // $product    = $this->_initProduct();
        // $model      = $this->_initSendToFriendModel();

        // if (!$product) {
        //     $this->_forward('noRoute');
        //     return;
        // }

        // if ($model->getMaxSendsToFriend() && $model->isExceedLimit()) {
        //     Mage::getSingleton('catalog/session')->addNotice(
        //         $this->__('The messages cannot be sent more than %d times in an hour', $model->getMaxSendsToFriend())
        //     );
        // }

        // $this->loadLayout();
        // $this->_initLayoutMessages('catalog/session');

        // Mage::dispatchEvent('sendfriend_product', array('product' => $product));
        // $data = Mage::getSingleton('catalog/session')->getSendfriendFormData();
        // if ($data) {
        //     Mage::getSingleton('catalog/session')->setSendfriendFormData(true);
        //     $block = $this->getLayout()->getBlock('sendfriend.send');
        //     if ($block) {
        //         $block->setFormData($data);
        //     }
        // }

        // $this->renderLayout();
    }

    /**
     * Send Email Post Action
     *
     */
    public function sendmailAction()
    {
        if (!$this->_validateFormKey()) {
            return $this->_redirect('*/*/send', array('_current' => true));
        }

        $product    = $this->_initProduct();
        $model      = $this->_initSendToFriendModel();
        $data       = $this->getRequest()->getPost();

        if (!$product || !$data) {
            $this->_forward('noRoute');
            return;
        }

        $categoryId = $this->getRequest()->getParam('cat_id', null);
        if ($categoryId) {
            $category = Mage::getModel('catalog/category')
                ->load($categoryId);
            $product->setCategory($category);
            Mage::register('current_category', $category);
        }

        $model->setSender($this->getRequest()->getPost('sender'));
        $model->setRecipients($this->getRequest()->getPost('recipients'));
        $model->setProduct($product);

        try {
            $validate = $model->validate();
            if ($validate === true) {
                $model->send();
                Mage::getSingleton('catalog/session')->addSuccess($this->__('The link to a friend was sent.'));
                $this->_redirectSuccess($product->getProductUrl());
                return;
            }
            else {
                if (is_array($validate)) {
                    foreach ($validate as $errorMessage) {
                        Mage::getSingleton('catalog/session')->addError($errorMessage);
                    }
                }
                else {
                    Mage::getSingleton('catalog/session')->addError($this->__('There were some problems with the data.'));
                }
            }
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('catalog/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('catalog/session')
                ->addException($e, $this->__('Some emails were not sent.'));
        }

        // save form data
        Mage::getSingleton('catalog/session')->setSendfriendFormData($data);

        $this->_redirectError(Mage::getURL('*/*/send', array('_current' => true)));
    }
}
1
  • is there any suggestion for this type of problem.
    – RickyN73
    Oct 13, 2015 at 11:12

1 Answer 1

2

There are few issue on ur code:

On before you need to put the module name instead of controller name:

before="Mage_Sendfriend"

also need to change overrider

Companyname_Sendfriend_Product to Companyname_Sendfriend

may be space issue it will be in one line

<Companyname_sendfriend before="Mage_Sendfriend_ProductController"> Companyname_Sendfriend_Product </Companyname_Sendfriend>

It should be

maybe space issue it will be in one line

<Companyname_sendfriend before="Mage_Wishlist">Companyname_Sendfriend</Companyname_sendfriend>

Now config.xml is

<?xml version="1.0"?> <config> <modules> <Companyname_Sendfriend> <version>1.6.0.0</version> </Companyname_Sendfriend> </modules> <frontend> <routers> <sendfriend> <args> <modules> <Companyname_sendfriend before="Mage_Sendfriend">Companyname_Sendfriend</Companyname_Sendfriend> </modules> </args> </sendfriend> </routers> </frontend> </config>

Class name is wrong:

Your Override class name is wrong:

Ir should be Companyname_Sendfriend_ProductController from

Xwalker_Sendfriend_ProductController

If you controller location is

app/code/local/Companyname/Sendfriend/controller

8
  • Thanks for your answer i will make the necessary changes and let you know.
    – RickyN73
    Oct 13, 2015 at 11:23
  • still not working it is redirecting me to 404 no route page
    – RickyN73
    Oct 13, 2015 at 11:27
  • can you please tell me you file path location
    – Amit Bera
    Oct 13, 2015 at 11:31
  • there are some space wil in my config.xml... i have modify .. Now please check aagin
    – Amit Bera
    Oct 13, 2015 at 11:31
  • file path location for config.xml \app\code\local\Companyname\Sendfriend\etc
    – RickyN73
    Oct 13, 2015 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.