0

I try to find a solution, I have a special case, Let's say that my customers add something in cart, then they need to login or register. So I want to do something like this: after login/registration if they have a product in cart to be redirected to the checkout page, if they don't have a product in cart to be redirected to the my account page. Is this possible? Or exist for Magento a extension like this?

Thank you

1 Answer 1

1

Yes, its possible.

OPTION 1: Using event observer, add below code in your config.xml:

<events>
    <customer_login>
        <observers>
            <redirection>
                <class>redirection/observer_customer</class>
                <method>customerLogin</method>
            </redirection>
        </observers>
    </customer_login> 
</events>

And in your customerLogin() of Namespace_Extension_Model_Observer_Customer class you need to check if current cart has any item then redirect to shopping cart page like -

public function customerLogin(Varien_Event_Observer $observer)
{    
    if (Mage::getModel('checkout/cart')->getQuote()->getItemsCount()){  
        $_session = Mage::getSingleton('customer/session');
        $_session->setBeforeAuthUrl(Mage::helper('checkout/cart')->getCartUrl());
    }
}

OPTION 2: Another option is to override Mage_Customer controller. In your module config.xml file add below code:

<frontend>
    <routers>
        <customer>
            <args>
                <modules>
                    <moduleidentifier_customer before="Mage_Customer">YourModule_Customer</moduleidentifier_customer>
                </modules>
            </args>
        </customer>
    </routers>
</frontend>

After that you have to create account controller inside your module controllers directory and placed below code in your controller class:

class Yourmodule_Customer_AccountController extends Mage_Customer_AccountController
{

   /**
     * Overriding defaults redirect URL 
     * Define target URL and redirect customer after logging in
     */
    protected function _loginPostRedirect()
    {    
        $session = $this->_getSession();

        if($session->isLoggedIn() && Mage::getModel('checkout/cart')->getQuote()->getItemsCount()){
            $_session->setBeforeAuthUrl(Mage::helper('checkout/cart')->getCartUrl());
        }
        else{
            if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {
                // Set default URL to redirect customer to
                $session->setBeforeAuthUrl($this->_getHelper('customer')->getAccountUrl());
                // Redirect customer to the last page visited after logging in
                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 = $this->_getModel('core/url')
                                ->getRebuiltUrl( $this->_getHelper('core')->urlDecodeAndEscape($referer));
                            if ($this->_isUrlInternal($referer)) {
                                $session->setBeforeAuthUrl($referer);
                            }
                        }
                    } else if ($session->getAfterAuthUrl()) {
                        $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                    }
                } else {
                    $session->setBeforeAuthUrl( $this->_getHelper('customer')->getLoginUrl());
                }
            } else if ($session->getBeforeAuthUrl() ==  $this->_getHelper('customer')->getLogoutUrl()) {
                $session->setBeforeAuthUrl( $this->_getHelper('customer')->getDashboardUrl());
            } else {
                if (!$session->getAfterAuthUrl()) {
                    $session->setAfterAuthUrl($session->getBeforeAuthUrl());
                }
                if ($session->isLoggedIn()) {
                    $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                }
            }
        }
        $this->_redirectUrl($session->getBeforeAuthUrl(true));       
    }
} 

Note: Compare your Mage_Customer_AccountController class _loginPostRedirect() method with above mentioned _loginPostRedirect() method for difference. Change module, method, configuration node, etc. according to your module.

Let me know if you have any issue regarding approach.

6
  • thank you so much, is there any way to put this in an extension please?
    – Robert
    Commented Feb 13, 2016 at 11:56
  • You may use any third party extension for Customer Redirection and put your redirection code inside their code. Magento Connect has soo many extension for redirect login which you get on this link . Commented Feb 13, 2016 at 12:00
  • give me few hour to try this solution
    – Robert
    Commented Feb 13, 2016 at 12:37
  • Sure, take you time ..... :) Commented Feb 13, 2016 at 12:42
  • Hi Jayesh Patel can you tell me something, your solution is almost perfect, can I make this check on the Proceed to Cart button? For example if the user is not login to be redirect to customer/account/login/
    – Robert
    Commented Feb 13, 2016 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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