How I can determine if the page is checkout/cart?

The question:

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

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You need to add below code of file
appdesignfrontendYour-ThemeYour-Packagetemplatepersistentcustomerformlogin.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));
            }
    }

Method 2

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

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment