The Overriding Mage_Wishlist_IndexController::addAction() in Magento Problem
You’re on the right track towards overriding the addAction method of the WishList Index Controller in Magento. The goal is to redirect back to the product page after adding a product to the wishlist instead of defaulting to the wishlist page. Let’s dive into your current approach and identify the areas that need adjustment.
The Configuration Setup
In your config.xml file under the MyCompany_Coreextensions folder, you’ve correctly configured the module with version 0.1.0. The frontend configuration specifies a new router for ‘wishlist’ and adds your custom module before the Mage_Wishlist module in the arguments section. This setup ensures that Magento will first look for your custom controller when processing wishlist actions.
The Controller Override
You’ve also created the IndexController.php file under the Wishlist folder within Coreextensions. In this file, you’re extending the base class and redefining the _addItemToWishList method. The core logic is intactâyou’re handling product validations, adding items to the wishlist, saving changes, and displaying success messages.
However, there are a few issues in your override that need addressing:
-
The redirect logic: In the original controller, after successfully adding an item to the wishlist, Magento redirects back to the wishlist page using
addActionReferer(). You’ve commented out this line and attempted to use_redirectReferer(), which is a private method of the base class. -
The comment in your code: You’re commenting that you want to stay on the product page after adding items to the wishlist, but your actual logic still redirects back to the wishlist or the referrer URL (which could be any page).
A Proper Solution
To achieve the desired behavior of staying on the product page, you need to modify the redirect logic in your override. Here’s how you can do it:
-
Uncomment the
addActionReferer()line and modify the$referervariable before calling this method. -
In the line where you set the referer URL, change it to the current product page URL instead of using
$this->_getRefererUrl().
Here’s an updated version of your _addItemToWishList method with these changes:
protected function _addItemToWishList()
{
// ... (validation and wishlist addition code remains unchanged)
$referer = Mage::helper('core/url')->getCurrentUrl();
if ($referer) {
// Set referer to the current product page URL
$session->setAddActionReferer($referer);
}
// ... (rest of the method remains unchanged)
}
In this version, we use Mage::helper(‘core/url’)->getCurrentUrl() to get the URL of the current page. This should return the URL of the product page where you’re adding items to the wishlist.
Conclusion
The key to successfully overriding a Magento core controller is understanding the original method’s flow and making the necessary adjustments in your override without directly modifying the original code. By following this approach, you can maintain the flexibility and safety provided by Magento’s extension architecture.
Remember that when working with Magento 2, it’s crucial to use proper module naming conventions and follow the official guidelines for extending core functionality. This ensures compatibility with future updates and maintains a clean separation between your custom code and the Magento core.
For more detailed information on Magento controller overrides and the best practices involved, I recommend checking out the official Magento documentation or visiting our blog at LaravelCompany.com for in-depth tutorials and insights from experienced Magento developers.
Happy coding!