2

I have site using Magento 1.7, and have as a first page a login that is mandatory.

I implemented the mandatory login via an observer, that is in charge of only allowing acces to the login page while the user is not logged in.

My code for the Observer is below:

public function checkForLogin(Varien_Event_Observer $observer)
{
  $allow = array('customer_account_login',
   'customer_account_createpost',
    'customer_account_create',
    'customer_account_forgotpassword',
    'customer_account_resetpassword',
    'customer_account_loginpost',
    'customer_account_forgotpasswordpost',
    'customer_account_resetpasswordpost');

  $currentURL = Mage::helper('core/url')->getCurrentUrl();
  $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
  $path = $url->getPath();
  $contactPaths = array('/mystore/index.php/es/contacts/');

   //Check if any customer is logged in or not, or if he is clicking the contacts page
   if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) && !in_array($path,$contactPaths)) 
    {
    Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
    //echo var_dump($observer->getControllerAction()->getFullActionName());
    exit;
   }
 }
}

The problem is that I want to allow the user to access the contacts page, even if he/she is not logged in. And that's why I included the use of getCurrentURL(), getPath(), and added a condition inside the if().

But I still can't access the contact pages. I am not sure of the behavior of getCurrentUrl(). What I need to get is the URL of the button that the user just clicked on; but maybe getCurrentUrl() just returns the URL of the current page, as its name suggests.

Is there any method available to get the URL of a clicked button? Or maybe I am missing something?

Any help is much appreciated.

  • 1
    Try using the core way like $_SERVER to get the URL and for redirect try to use this Mage::app()->getFrontController()->getResponse()->setRedirect($url); – Ashish Ranade Jan 12 '17 at 12:52
2

If you want to allow access to contact us page just add contacts_index_index in $allow array and undo the condition for $contactPaths

if you want to add any other url in the array you can get the Full action name by calling this code on that page

echo Mage::app()->getFrontController()->getAction()->getFullActionName()
  • Perfect, so simple and it works. Thanks. Where can I find a list with all the possibilities, in case I need again? This 'contacts_index_index' is an Action Name right? – LeCodeur Jan 12 '17 at 13:34
1

Replace your code with this :

<?
public function checkForLogin(Varien_Event_Observer $observer)
{
  $allow = array('customer_account_login',
   'customer_account_createpost',
    'customer_account_create',
    'customer_account_forgotpassword',
    'customer_account_resetpassword',
    'customer_account_loginpost',
    'customer_account_forgotpasswordpost',
    'customer_account_resetpasswordpost');

  $currentURL = Mage::helper('core/url')->getCurrentUrl();
  $path = explode('/',$currentURL);
  //Check if any customer is logged in or not, or if he is clicking the contacts page
   if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) && !in_array('contacts',$path)) 
    {
    Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
    //echo var_dump($observer->getControllerAction()->getFullActionName());
    exit;
   }
 }
}

Here is good module for this get here.

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.