0

I am trying to redirect a specific customer group to a single CMS page. Not just after logging in, but whenever they try to go to any page, it will just redirect them to the CMS page.

The only file I figured I could add code is in header.phtml.

$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

    if($customerGroupId == 5){

            $redirect_url = Mage::getUrl('uhoh');
         Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url)->sendResponse();
    }

This is what I currently have. It's purpose works, but it's not exactly what I want. The purpose is for a "blocked" customer group. This code sends the user into an endless redirect, but I want to have information displayed on the CMS page, which means no page is useless despite actually blocking the group from using the site.

Is there an issue with the code? Or would it be easier and more effective to create a module for this?

  • 1
    To stop the infinite loop you might be able to add another condition to your if statement not to fire the redirect if current url == target url. You can get current url using $currentUrl = Mage::helper('core/url')->getCurrentUrl(); – AJ47 Mar 31 '18 at 14:19
  • Thanks for the comment, after thinking about it, this made complete sense. I tried doing if(!$currentUrl == $url) { //redirect} but it failed. However, if($currentUrl == $url) {} else { // redirect } worked perfectly. Thanks so much! – taskle Mar 31 '18 at 14:33
  • No problem, the first example would work like this if($currentUrl != $url) { //redirect} but glad you got it to work! – AJ47 Mar 31 '18 at 14:37
  • 1
    Yeah, I realized I just messed up the operator location. Or !($currentUrl == $url) could have worked. – taskle Mar 31 '18 at 14:52
1

To stop the infinite loop from happening, add a 2nd condition to your IF statement checking the current URL:

$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$redirect_url = Mage::getUrl('uhoh');

    if($customerGroupId == 5 && $currentUrl !== $redirect_url ){  
         Mage::app()->getFrontController()->getResponse()->setRedirect($redirect_url)->sendResponse();
    }

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.