Search code examples
magentocontrolleroverridingoverloading

Overriding Mage_Wishlist_IndexController::addAction() in Magento


I am having trouble overriding one of Magento's core controllers, the WishList Index Controller. When I add a product in the wishlist, I need Magento to redirect back to the product page instead of the wishlist. Here is what I did so far

  1. Created a folder in app/code/local, called MyCompany, with a subfolder called Coreextensions.
  2. Inside the Coreextensions folder, I created etc/config.xml, with the following content:

No, this instance is intended for use outside of production or under the RDS Free Usage Tier

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_Coreextensions>
            <version>0.1.0</version>
        </MyCompany_Coreextensions>
    </modules>
 
    <frontend>
        <routers>
            <wishlist>
                <args>
                    <modules>
                        <MyCompany_Coreextensions before="Mage_Wishlist">
                            MyCompany_Coreextensions_Wishlist
                        </MyCompany_Coreextensions>
                    </modules>
                </args>
            </wishlist>
        </routers>
    </frontend>
</config>
  1. Inside Coreextensions folder, I created controllers/Wishlist/IndexController.php, like this:
  <?php
    /* Stay on product page after adding to wishlist */
  require_once(Mage::getModuleDir('controllers','Mage_Wishlist').DS.'IndexController.php');
    
    class MyCompany_Coreextensions_Wishlist_IndexController extends Mage_Wishlist_IndexController
    {
      
    /**
     * Add the item to wish list
     *
     * @return Mage_Core_Controller_Varien_Action|void
     */
    protected function _addItemToWishList()
    {
        $wishlist = $this->_getWishlist();
        if (!$wishlist) {
            return $this->norouteAction();
        }
    
        $session = Mage::getSingleton('customer/session');
    
        $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 {
            $requestParams = $this->getRequest()->getParams();
            if ($session->getBeforeWishlistRequest()) {
                $requestParams = $session->getBeforeWishlistRequest();
                $session->unsBeforeWishlistRequest();
            }
            $buyRequest = new Varien_Object($requestParams);
    
            $result = $wishlist->addNewItem($product, $buyRequest);
            if (is_string($result)) {
                Mage::throwException($result);
            }
            $wishlist->save();
    
            Mage::dispatchEvent(
                'wishlist_add_product',
                array(
                    'wishlist' => $wishlist,
                    'product' => $product,
                    'item' => $result
                )
            );
    
            $referer = $session->getBeforeWishlistUrl();
            if ($referer) {
                $session->setBeforeWishlistUrl(null);
            } else {
                $referer = $this->_getRefererUrl();
            }
    
            /**
             *  Set referer to avoid referring to the compare popup window
             */
            $session->setAddActionReferer($referer);
    
            Mage::helper('wishlist')->calculate();
    
            $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.',
                $product->getName(), Mage::helper('core')->escapeUrl($referer));
            $session->addSuccess($message);
        } catch (Mage_Core_Exception $e) {
            $session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
        }
        catch (Exception $e) {
            $session->addError($this->__('An error occurred while adding item to wishlist.'));
        }
    
        //$this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
        $this->_redirectReferer();
    }
    
    }
  1. In app/etc/modules, I created MyCompany_Coreextensions.xml, like this:

     <?xml version="1.0"?>
     <!--we need to enable this module as any other if-->
     <!--you wish to do it as standalone module extension-->
     <config>
         <modules>
             <MyCompany_Coreextensions>
                 <active>true</active>
                 <codepool>local</codepool>
             </MyCompany_Coreextensions>
         </modules>
     </config>
    

Of course, this doesn't work and it's driving me nuts. If I make the change in the Core file, it works as I want it to, but I wouldn't want to alter the Core files... Let me say that YES, I did clear the cache!


Solution

  • Your steps are right but only two things:

    1. you have to add this row require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php'; first of Controller class declaration;
    2. the name of your Controller class must be without '_Wishlist' so MyCompany_Coreextensions_IndexController instead of MyCompany_Coreextensions_Wishlist_IndexController (you have to change it into config.xml too -> <MyCompany_Coreextensions before="Mage_Wishlist">MyCompany_Coreextensions</MyCompany_Coreextensions> instead of <MyCompany_Coreextensions before="Mage_Wishlist">MyCompany_Coreextensions_Wishlist</MyCompany_Coreextensions>)