4

This question already has an answer here:

My event code in config.xml

<events>
          <controller_action_predispatch>
            <observers>
              <Test_Redirect_Model_Observer>
                <type>singleton</type>
                <class>redirect/observer</class>
                <method>redirect</method>
              </Test_Redirect_Model_Observer>
            </observers>
          </controller_action_predispatch>
        </events>

My Observer.php Code

class Test_Redirect_Model_Observer extends Varien_Event_Observer{

    public function redirect($observer){

        $session = Mage::getSingleton("customer/session");

        try{ 


            if($session->isLoggedIn()){

                $someUrl = "SOME URL";
                Mage::app()->getFrontController()->getResponse()->setRedirect($someUrl)->sendResponse();
                exit;


            }else{

                $someOtherUrl = "SOME OTHER URL";
                Mage::app()->getFrontController()->getResponse()->setRedirect($someOtherUrl)->sendResponse();
                exit;
            }
        }

        catch(Exception $e) {
        Mage::logException($e);
       } 

   }

}

Please guide me on why I am stuck in Infinite Loop when redirect from Observer.

Thanks.

marked as duplicate by David Manners, user487772 May 23 '14 at 5:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You are getting the redirect because your code is always called. You need some kind of restriction for when the redirect is not made. Take a look at this: magento.stackexchange.com/a/15890/146. I think it's something similar to what you need. – Marius Mar 12 '14 at 9:58
  • I just need to redirect to login page if customer not logged in & redirect to cart page if logged in..How can I achieve this with the above code and what restrictions I need to put for the same ? – Mark Mar 12 '14 at 10:08
  • Redirecting a not logged in customer to the login is exactly what the code does in the link I gave you. And if the customer is logged in and you always redirect to the cart how can he perform other actions? Like adding a product to the cart. – Marius Mar 12 '14 at 11:01
  • @Marius I agree with you. I was just testing out the code and thanks for the link. – Mark Mar 12 '14 at 11:31
0

I suggest you override AccountController.php's _loginPostRedirect() near

if ($session->isLoggedIn()) {
            if (!Mage::getStoreConfigFlag(
                Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
            )) {
                $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                if ($referer) {
                    // Rebuild referer URL to handle the case when SID was changed
                    $referer = Mage::getModel('core/url')
                        ->getRebuiltUrl(Mage::helper('core')->urlDecode($referer));
                    if ($this->_isUrlInternal($referer)) {
                        $session->setBeforeAuthUrl($referer);
                    }
                }
            } else if ($session->getAfterAuthUrl()) {
                $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            }
        } else {
            $session->setBeforeAuthUrl(Mage::helper('customer')->getLoginUrl());
        }
  • 1
    The _loginPostRedirect() will only be called when I explicitly login. I need to redirect user to login page is not logged in and redirect to cart page if logged in. – Mark Mar 12 '14 at 10:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.