Magento Redirect to Product Page After Add to Wishlist for Unauthenticated Users
In this tutorial, we’ll guide you through configuring Magento to redirect users who are not logged in to the login page when they attempt to add an item to their wishlist. Once authenticated, we’ll ensure that the user is directed back to the product page from which they initiated the wishlist action.
Understanding the Scenario
When a guest (unauthenticated) user tries to add a product to their wishlist in Magento, the platform naturally redirects them to the My Account dashboard upon successful login. However, this behavior isn’t always ideal for users who were actively browsing products and only wanted to save an item for later consideration.
The Magento Wishlist Redirect Process
To achieve our desired flow, we’ll need to modify two key aspects of Magento’s wishlist functionality:
- Redirect unauthenticated users to the login page when adding a product to their wishlist.
- Implement custom logic to redirect authenticated users back to the recently viewed product after logging in.
Step 1: Redirect Unauthenticated Users
By default, Magento uses the Mage_Customer_Helper_Data::getLoginUrl() function to redirect unauthenticated users. However, this function redirects them to the login page with an empty referer URL, which can cause issues with some themes or customizations.
To ensure a proper redirection, we’ll override this function and provide the necessary context for the referral.
Override getLoginUrl() Function
Navigate to your theme’s local copy of app/design/frontend/[yourtheme]/template/customer/persistent/customer/form/login.phtml. In this file, locate the line where the login URL is generated using getLoginUrl().
$login_url = Mage::helper('customer')->getLoginUrl();
Replace the above line with the following code:
$refererUrl = $this->getReferrerUrl();
$login_url = Mage::getUrl('customer/account/login', array('_secure' => true, '_query' => 'referer=' . urlencode($refererUrl)));
The getReferrerUrl() function should be defined in your template as follows:
public function getReferrerUrl()
{
return $this->getRequest()->getServer('HTTP_REFERER');
}
This modification ensures that the login URL contains a referer parameter pointing to the page where the user attempted to add an item to their wishlist.
Step 2: Redirect Authenticated Users
After modifying the form/login.phtml file, we still need to handle the redirection of authenticated users back to the recently viewed product. Magento’s core functionality currently redirects these users to the My Account dashboard post-login.
To achieve our desired behavior, we’ll create an observer that listens for the customer_auth_success event and handles the redirection accordingly.
Create a Custom Observer
In your custom module (or directly in your theme if preferred), create an etc/config.xml file with the following contents:
<config>
<modules>
<Your_Module_Name>
<version>1.0.0</version>
</Your_Module_Name>
</modules>
<global>
<events>
<customer_auth_success>
<observers>
<your_module_name_customer_auth_success_observer>
<type>singleton</type>
<class>Your_Module_Name_Model_Observer::authSuccess</class>
<method>redirectToProductUrl</method>
</your_module_name_customer_auth_success_observer>
</observers>
</customer_auth_success>
</events>
</global>
</config>
Next, create the observer class Your_Module_Name/Model/Observer.php:
class Your_Module_Name_Model_Observer
{
public function authSuccess($observer)
{
$productId = $this->getRequest()->getParam('product');
if ($productId) {
$product = Mage::getModel('catalog/product')->load($productId);
if ($product && $product->isVisible()) {
$this->_redirectToProductUrl($product);
}
}
return $this;
}
private function _redirectToProductUrl($product)
{
$url = $product->getProductUrl();
$this->_redirect($url);
}
}
The observer captures the customer_auth_success event, retrieves the product ID from the request parameters (if available), and redirects the authenticated user to the corresponding product URL.
Conclusion
By following this comprehensive guide, you should now be able to configure Magento to redirect unauthenticated users to the login page when adding products to their wishlist and ensure that authenticated users are directed back to the recently viewed product after a successful login.
This customization provides a seamless user experience for customers browsing your Magento store while maintaining the security of requiring authentication before allowing wishlist functionality.