Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

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:  1771
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

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
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:  1771
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>

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
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:  1771
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.

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
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
Magento Community
Magento Community
    Back to top
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
53207 users|773 users currently online|107246 forum posts