Magento – Custom Redirection


In Magento, to Perform a Custom Redirection, we can use the setRedirect() Method of front controller.

For example, if on certain page we want to check that if customer is logged in then only customer can access that page.  For such scenario, first we can check if customer is logged in or not using the isLoggedIn() method and then we can set the current Page URL that can be used if customer goes to login page and successfully logged in in the magento store. that can be achieved using setBeforeAuthUrl() method.

i.e. Currently user is not logged in and is on the category page. We want functionality that if user is logged-in then only he/she can view the full product details page. So when user clicks on product link for viewing the detailed page, he will be redirected to the Customer Login Page. If customer successfully authenticated then again he/she will be redirected on the same page where he was previously. this can be implemented with the help of setBeforeAuthUrl() method.

Example :

//Check If Customer Is Logged In Or Not.
if (!Mage::getSingleton("customer/session")->isLoggedIn())
{
$session = Mage::getSingleton("customer/session");
// Store The Current Page Url Where User will be redirected once loggedin
$session->setBeforeAuthUrl(Mage::helper("core/url")->getCurrentUrl());
$customerLoginURL = $this->getBaseUrl() . "customer/account/login";
Mage::app()->getFrontController()->getResponse()->setRedirect($customerLoginURL)->sendResponse();
}

Have Fun 😉

Advertisements

2 comments on “Magento – Custom Redirection

  1. Pingback: 2010 in review « LAMP Ville

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s