Determine if Page is Checkout or Cart in Magento 1.9 – How to Guide

magento-1.9onepage-checkoutPHPshopping-cart-price-rules

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

Best Answer

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));
            }
    }
Related Topic