Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Overriding IndexController.php in Wishlist
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

Hi,

I want to override the addAction() function in the IndexController of the Wishlist. I followed the Wiki steps on how to overload the controllers.

I created a MyNameSpace_All.xml in app/etc/modules in which I added:

<?xml version="1.0"?>
  
<config>
      <
modules>
          <
MyNameSpace_MyModule>
              <
active>true</active>
              <
codePool>local</codePool>
          </
MyNameSpace_MyModule>
      </
modules>
  </
config>

Then I created app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:

<?xml version="1.0"?>
 
<config>
     <
modules>
         <
MyNameSpace_MyModule>
             <
version>0.1.0</version>
         </
MyNameSpace_MyModule>
     </
modules>
     <global>
         <
rewrite>
             <
mynamespace_mymodule_wishlist_index>
                 <
from><![CDATA[#^/wishlist/$#]]></from>
                  
<to>/mymodule/wishlist_index/</to>
              </
mynamespace_mymodule_wishlist_index>
          </
rewrite>
      </global>
      <
frontend>
          <
routers>
              <
mynamespace_mymodule>
                  <use>
standard</use>
                  <
args>
                      <
module>MyNameSpace_MyModule</module>
                      <
frontName>wishlist</frontName>
                  </
args>
              </
mynamespace_mymodule>
          </
routers>
      </
frontend>
</
config>

Then I created app/code/local/MyNameSpace/MyModule/Wishlist/IndexController.php with the following content:

<?php
#
# Controllers are not autoloaded so we will have to do it manually:
#
require_once 'Mage/Wishlist/controllers/IndexController.php';

class 
MyNameSpace_MyModule_Wishlist_IndexController extends Mage_Wishlist_IndexController
{
    
# Overloaded
    
public function addAction()
    
{
    
        error_log
('CUSTOM ACTION!');
    
        
$session Mage::getSingleton('customer/session');
        
$wishlist $this->_getWishlist();
        if (!
$wishlist{
            $this
->_redirect('*/');
            return;
        
}

        $productId 
= (int) $this->getRequest()->getParam('product');
        if (!
$productId{
            $this
->_redirect('*/');
            return;
        
}

        $product 
Mage::getModel('catalog/product')->load($productId);
        if (!
$product->getId() || !$product->isVisibleInCatalog()) {
            $session
->addError($this->__('Cannot specify product'));
            
$this->_redirect('*/');
            return;
        
}

        
try {
            $wishlist
->addNewItem($product->getId());
            
Mage::dispatchEvent('wishlist_add_product', array('wishlist'=>$wishlist'product'=>$product));

            if (
$referer $session->getBeforeWishlistUrl()) {
                $session
->setBeforeWishlistUrl(null);
            
}
            
else {
                $referer 
$this->_getRefererUrl();
            
}
            $message 
$this->__('%1$s was successfully added to your wishlist. Click <a href="%2$s">here</a> to continue shopping'$product->getName(), $referer);
            
$session->addSuccess($message);
        
}
        
catch (Mage_Core_Exception $e{
            $session
->addError($this->__('There was an error while adding item to wishlist: %s'$e->getMessage()));
        
}
        
catch (Exception $e{
            $session
->addError($this->__('There was an error while adding item to wishlist.'));
        
}
        $this
->_redirect('*');
    
}
    
}

?>

Then I modified app/design/frontend/default/default/layout/wishlist.xml

<!--
Wishlist pages
-->



    <
wishlist_index_index>
        <!-- 
Mage_Wishlist -->
        <
update handle="customer_account"/>
        <
reference name="content">
            <
block type="wishlist/customer_wishlist" name="customer.wishlist" template="wishlist/view.phtml"/>
        </
reference>
        <
reference name="right">
            <
action method="unsetChild"><name>wishlist_customer_sidebar</name></action>
        </
reference>
    </
wishlist_index_index>
    
<!-- NEW 
MODULE -->
    <
mymodule_wishlist_index_index>
        <
update handle="customer_account"/>
    </
mymodule_wishlist_index_index>

I am getting a page not found when I go to /wishlist/. What am I doing wrong? Which part of the XML of the layout was I supposed to modify. If there were already handlers there?

Please help since it’s getting really frustrating.

::Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

I’m getting an error:
Call to a member function setRefererUrl() on a non-object in /var/www/test/app/code/core/Mage/Wishlist/controllers/IndexController.php on line 64

It seems that I am not setting the path correctly. What do I need to enter into the XML files when I need to override the IndexController? The wiki only mentions checkout/cart but that’s different.

Anyone?

::Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Then I created app/code/local/MyNameSpace/MyModule/Wishlist/IndexController.php with the following content:

I think it should be app/code/local/MyNameSpace/MyModule/controllers/Wishlist/IndexController.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

Hi,

Sorry for the mistype, the directory is actually app/code/local/MyNameSpace/MyModule/controllers/Wishlist with the file IndexController.php. It seems that either the wishlist.xml is incorrect or the config.xml of the new module. Is there an error in them? Would I need to copy more files over to the overriden class’s directory? I know that the default is the IndexController, however how do I override that. Either my rewrite is incorrect or the way I update the wishlist.xml file.

please help

Thanks,

::Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

You’ve declared:

<routers>
              <
mynamespace_mymodule>
                  <use>
standard</use>
                  <
args>
                      <
module>MyNameSpace_MyModule</module>
                      <
frontName>wishlist</frontName>
                  </
args>
              </
mynamespace_mymodule>
          </
routers>
Try <frontName>mymodule</frontName>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

Hi,

I modified it, I’m still getting: Fatal error: Call to a member function setRefererUrl() on a non-object in /var/www/test/app/code/core/Mage/Wishlist/controllers/IndexController.php on line 64

Here is the new config.xml:

<?xml version="1.0"?>
 
<config>
     <
modules>
         <
MyNameSpace_MyModule>
             <
version>0.1.0</version>
         </
MyNameSpace_MyModule>
     </
modules>
  
  <global>
    <
rewrite>
    <
mynamespace_mymodule_wishlist_index>
                 <
from><![CDATA[#^/wishlist/$#]]></from>
                  
<to>/mymodule/wishlist_index/</to>
              </
mynamespace_mymodule_wishlist_index>
          </
rewrite>
      </global>
 
      <
frontend>
         <
routers>
              <
mynamespace_mymodule>
                <use>
standard</use>
                  <
args>
                      <
module>MyNameSpace_MyModule</module>
                      <
frontName>mymodule</frontName>
                  </
args>
              </
mynamespace_mymodule>
          </
routers>
      </
frontend>
</
config>

The derived IndexController is located in:  /var/www/test/app/code/local/MyNameSpace/MyModule/controllers/Wishlist/

It seems that either the /var/www/test/app/design/frontend/default/default/layout/wishlist.xml’s content is wrong or the config.xml.

I also tried adding the namespace to the wishlist.xml entry, but still get the same error (with and without the namespace in the front):

<wishlist_index_index>
        <!-- 
Mage_Wishlist -->
        <
update handle="customer_account"/>
        <
reference name="content">
            <
block type="wishlist/customer_wishlist" name="customer.wishlist" template="wishlist/view.phtml"/>
        </
reference>
        <
reference name="right">
            <
action method="unsetChild"><name>wishlist_customer_sidebar</name></action>
        </
reference>
    </
wishlist_index_index>
    
<!-- NEW 
MODULE -->
    <
mynamespace_mymodule_wishlist_index_index>
        <
update handle="customer_account"/>
    </
mynamespace_mymodule_wishlist_index_index>

Am I missing something? Or did I do something wrong?

::Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

First, it looks like the original controller action is being dispatched still.

The line you get the error on:

$this->getLayout()->getBlock('customer.wishlist')->setRefererUrl($this->_getRefererUrl());
Apparently this controller expects to have customer.wishlist block, try replacing it with:
if ($block $this->getLayout()->getBlock('customer.wishlist')) {
   $block
->setRefererUrl($this->_getRefererUrl());
}
And see which page components you miss.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

Hi,

I added the code to the app/code/core/Mage/Wishlist/controllers/IndexController.php’s indexAction.

now it doesn’t give an error but the wishlist is not displayed after the “Add to Wishlist” is clicked. Do I need to override the indexAction as well in the MyModule?

I also tried var_dump($block) after the if clause and it returned false (this was added to the app/code/core version).

::Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sintax
Member
 
Total Posts:  58
Joined:  2008-02-18
Hungary
 

The item is added to the wishlist however it is not displayed. Maybe the old one is still being called.

Any ideas?

:Sintax

 
Magento Community Magento Community
Magento Community
Magento Community
 
thesharing_net
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2010-01-17
 

Hi all

I want to make a new custom module (Paymember) as Wishlist module. function as wishlist but not overide. I had copy a new wishlist and custom it but when I click the my button as wishlist link. It error: Fatal error: Class ‘Mage_Paymember_Helper_Data’ not found in E:\wamp\www\magento3\app\Mage.php on line 520

what is error, Can you help me ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mspavkovic
Jr. Member
 
Total Posts:  6
Joined:  2009-12-06
 

I have a similar problem when ovverriding wisahlist index controller - the index method.
Local changes in index controller are applied but layout is not loaded so only blank page is shown. Problem is that right handle is not loaded
instead wishlist_index_index handle wishlist_wishlist_index_index is loaded!!!!
My changes are

config.xml

<rewrite>
<namespace_module_wishlist_index>
<rewrite /wishlist/index requests >

<from><![CDATA[#^/wishlist/$#]]></from>
<to>/module/wishlist_index/${1}/</to>
</namespace_module_wishlist_index>
</rewrite>

IndexController

require_once ‘Mage/Wishlist/controllers/IndexController.php’;
class Namespace_Module_Wishlist_IndexController extends Mage_Wishlist_IndexController{

public function indexAction()
{
//some local changes
$this->loadLayout();
// layout is not loaded
$session = Mage::getSingleton(’customer/session’);
$block = $this->getLayout()->getBlock(’customer.wishlist’);
.......
}

Any idea what i am doing wrong.

Kind regards,
Miroslav

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top