Magento: Retrieving Previous URL Before Current Page
Introduction
In Magento, you may find yourself needing to retrieve the URL of the previous page that a customer visited before arriving at the current page. This is particularly useful when you want to check if the last visited page was the shopping cart or checkout page.
However, retrieving this information can be challenging due to Magento’s session management and the fact that URLs are often encoded or changed during the flow of operations such as login or registration.
In this post, we’ll guide you through a practical solution to this common problem, leveraging Magentoâs event observers and session handling mechanisms.
Understanding the Problem
The primary issue lies in retrieving the correct URL from the customer’s session. The getLastUrl() method only retrieves the last URL that triggered an action within the current session, which is often not the page you’re looking for, especially after login or registration events.
For example, consider this typical flow:
- Customer visits the shopping cart (
checkout/cart) - Proceeds to checkout (
onestepcheckout/index) - Redirects to the login page (
customer/account/login)
Even though the last URL before reaching the current page (login) should be the checkout URL, using getLastUrl() will return the login URL.
The Incorrect Approach
The poster initially tried to use a simple approach:
$lastUrl = Mage::getSingleton('core/session')->getLastUrl();
if(preg_match("#onestepcheckout/index#", $lastUrl)){
// Redirect logic
}
However, this fails because the getLastUrl() method returns the last page from which a customer action was originated (in this case, the login page), not necessarily the URL of the previous step in the flow.
The Correct Solution: Event Observers
The key to solving this problem is understanding that Magento’s event observers can be used to track and manage state across different pages and sessions. By setting up an observer before a customer registers or logs in, we can store the relevant URL and use it later.
Hereâs how you can implement this:
- Set up your config.xml:
<events>
<customer_login>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>setPreviousUrl</method>
</customerredirect>
</observers>
</customer_login>
<controller_action_predispatch>
<observers>
<customerredirect>
<class>customerredirect/observer_customer</class>
<method>storePreviousUrl</method>
</customerredirect>
</observers>
</controller_action_predispatch>
</events>
Here, we’re setting up an observer that runs before the login event (customer_login) and another that runs before every controller action (controller_action_predispatch). This latter observer is used to store the current URL in session when a customer navigates away from any page.
- Implement your observers:
public function setPreviousUrl(Varien_Event_Observer $observer)
{
Mage::getSingleton('core/session')->setPreviousUrl(Mage::helper('core/url')->getCurrentUrl());
}
public function storePreviousUrl(Varien_Event_Observer $observer)
{
if (!Mage::getSingleton('core/session')->getIsLoggedIn()) {
Mage::getSingleton('core/session')->setLastUrl(Mage::helper('core/url')->getCurrentUrl());
}
}
- Redirect after login or registration:
Now, when a customer logs in or registers, you can access the PreviousUrl session variable to check if they came from the checkout page:
public function customerLogin(Varien_Event_Observer $observer)
{
$previousUrl = Mage::getSingleton('core/session')->getPreviousUrl();
if (strpos($previousUrl, 'onestepcheckout/index') !== false) {
// Redirect logic to checkout
} else {
// Redirect logic to customer account
}
}
Conclusion
Retrieving the previous URL in Magento is more complex than it first appears due to the way Magento handles sessions and events. However, by using event observers and properly managing session variables across different URLs, you can achieve this effectively.
Remember, Magentoâs event observers are a powerful tool for customizing behavior and tracking state across multiple pages and customer actions. Don’t hesitate to explore them further when building your e-commerce solutions.
For more in-depth tutorials on Magento development, feel free to visit our blog at LaravelCompany.com.