How I can determine if the page is checkout/cart?
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
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));
}
}
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('customer/account'));
Mage::app()->getResponse()->sendResponse();
exit;
}
Mage::getSingleton('core/session')->setIsFromCart('0');
}
Thats all... good luck