4

I have something but this checks if in the cart are products:

  public function customerLogin(Varien_Event_Observer $observer)
   {    
           if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){    
               $lasturl = Mage::getSingleton('core/session')->getLastUrl();
             if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout/cart') === false){
                 if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
                          if($this->_CustomerGroup()) {
                           $_session = $this->_getSession();
                           $_session->setBeforeAuthUrl(Mage::helper('customerredirect')->setRedirectOnLogin());
                         }
                }
            }
         }
        Mage::getSingleton("core/session")->setRedirectregister(false);
   }

and I add this in the /app/design/frontend/base/default/template/checkout/onepage/link.phtml

<?php if ($this->isPossibleOnepageCheckout() && Mage::getSingleton('customer/session')->isLoggedIn()):?>
    <button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Proceed to Checkout')) ?>" class="button btn-proceed-checkout btn-checkout<?php if ($this->isDisabled()):?> no-checkout<?php endif; ?>"<?php if ($this->isDisabled()):?> disabled="disabled"<?php endif; ?> onclick="window.location='<?php echo $this->getCheckoutUrl() ?>';"><span><span><?php echo $this->__('Proceed to Checkout') ?></span></span></button>
<?php else: ?>
    <button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Proceed to Checkout')) ?>" class="button btn-proceed-checkout btn-checkout<?php if ($this->isDisabled()):?> no-checkout<?php endif; ?>"<?php if ($this->isDisabled()):?> disabled="disabled"<?php endif; ?> onclick="window.location='<?php echo Mage::getUrl('customer/account/login'); ?>';"><span><span><?php echo $this->__('Proceed to Checkout') ?></span></span></button>
<?php endif?>

All I want to do is when I press on the Proceed to Checkout if the user is not login to be redirected to login and the autoredirect to checkout onepage.

After more searches i found something that need to be put in the onepage checkout:

      if (!$this->helper('customer')->isLoggedIn()) {
             header("Location: /customer/account/login/");
             exit();
      }

but this is generally how I can implement this for my needs?

Edit: Maybe this can be a solution? https://magento.stackexchange.com/a/64085/22840 Thank you

  • For me, this does not make sense. Let's assume that the user(logged in user) has just arrived on checkout cart. In this moment, you want to automatically redirect him to checkout onepage, withought letting him to see the cart ? – Alex Constantinescu Feb 19 '16 at 18:17
  • Hi Alex, I edit my post now, please take a look again – Robert Feb 19 '16 at 18:18
  • So i need something like this, to make some checks if the user is not in the shopping cart page, then when he is login the redirect will be to My Account, if is in the shopping cart and login then must be redirected to onepage checkout – Robert Feb 19 '16 at 18:22
  • is ok? now make sense? – Robert Feb 19 '16 at 18:27
3

You need to add below code of file app\design\frontend\Your-Theme\Your-Package\template\persistent\customer\form\login.phtml

<?php if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout/cart') !== false) {
        Mage::getSingleton('core/session')->setIsFromCheckout('1');
      }
?>

Then you need to override function _loginPostRedirect of Mage_Customer_AccountController class

protected function _loginPostRedirect()
    {
        $session = $this->_getSession();

        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')->urlDecode($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));
            }
        }
        //add condition to check is from checkout page
        if(Mage::getSingleton('core/session')->getIsFromCheckout()){
                Mage::getSingleton('core/session')->unsIsFromCheckout();
                $this->_redirect('checkout/onepage');
            }else{          
                $this->_redirectUrl($session->getBeforeAuthUrl(true));
            }
    }
  • I use a custom extension for checkout I have Magestore OneStepCheckout, so is not work, this extension have own login popup, so i need to pass that login – Robert Feb 19 '16 at 18:49
  • link.phtml code is not working for you?Whats problem you are facing? – Prashant Valanda Feb 19 '16 at 18:53
  • the link is work, when the customers are not login, the button go in the login form, my problem is after they add username and password and press login, instead checkout they are redirected to my account – Robert Feb 19 '16 at 18:59
  • I have edited my answer please check and let me know if any – Prashant Valanda Feb 19 '16 at 19:32
  • hi but i don't use the default checkout – Robert Feb 19 '16 at 19:34
4

That is how you can check for the cart page

// Check for cart page
if (Mage::app()->getFrontController()->getAction()->getFullActionName() == 'checkout_cart_index') {
    // The current page is the cart
} else ... whatever you want to do then

To check if the cart is empty or not use

$isEmpty = Mage::getSingleton('checkout/session')->getQuote()->getItemsCount() > 0
  • hi thank you but is not work on my code – Robert Feb 19 '16 at 17:52
  • You can use this in your function and then it depends on what you want to do – Klettseb Feb 19 '16 at 17:54
  • I write there exactly how I want to be, can you made this for me please? – Robert Feb 19 '16 at 17:59
  • in fact the idea is like this, i need to check if the user is login and is in the checkout/cart, if it yes then redirect to checkout/onepage, if not the redirect to my account. – Robert Feb 19 '16 at 18:04
  • 1
    something like this? if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout/cart') === false){ – Robert Feb 19 '16 at 18:09
2

Working like a charm !

Add this to your /customer/form/login.phtml

<?php if (strpos(Mage::getSingleton('core/session')->getLastUrl(), 'checkout/cart') !== false) {
        Mage::getSingleton('core/session')->setIsFromCart('1');

      }
      else{
        Mage::getSingleton('core/session')->setIsFromCart('0');
      }
    ?>

then the observer:

public function customerLogin($observer)
     {
         if(Mage::getSingleton('core/session')->getIsFromCart() == 1){
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/onepage'));
            Mage::app()->getResponse()->sendResponse();
            exit;
         }

         else{ 
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('cust‌​omer/account'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
         Mage::getSingleton('core/session')->setIsFromCart('0');

     }

Thats all... good luck

  • Hi Alex i do this logic before, but my client won't this. he want when the customers is on the shopping cart, when they press Procced to Cart button to go on login and then to Checkout page directly page, otherwise if they are in other pages after login must go in My account – Robert Feb 19 '16 at 19:19
  • ok..i will try to implement it – Alex Constantinescu Feb 19 '16 at 19:22
  • thank you so much, for redirect the url's after login I use this extension Magehit_CustomLoginRedirect-0.0.1 and the checkout extension is Magestore OneStepCheckout – Robert Feb 19 '16 at 19:26
  • Please see my edited answer :) – Alex Constantinescu Feb 19 '16 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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