1

I have tried to redirect customer to a certain category after successful registration however I couldn't achieve it (even with the help in here, still didn't work stackexchange question)so I changed the plan to sort it out with redirecting the customer using $_SERVER['HTTP_REFERER'] and session variable where I put these in the header.phtml but this time I can't get the category URL for the custom category attribute. Anyone can help?

$session = Mage::getSingleton('customer/session');
if($session) {
    $customerclass = $session->getPasscode();
    if( $_SERVER['HTTP_REFERER'] == 'http://www.localhost.com/customer/account/create/' && $customerclass != ''){

        $categories = Mage::getModel('catalog/category')
                  ->getCollection()
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('passcode', $customerclass)
                  ->addIsActiveFilter()
                  ->addUrlRewriteToResult();

        if($categories->count()){
             $url = $categories->getFirstItem()->getUrl();
             Mage::app()->getFrontController()->getResponse()->setRedirect($url)->sendResponse();
             exit;
        }
    }
}
5
  • foreach($categories as $category){ $categories = Mage::getModel('catalog/category')->load($category->getId()); } What is this all about? Commented Dec 10, 2015 at 9:39
  • have your created custom category attribute? If created then what is attribute?
    – Abdul
    Commented Dec 10, 2015 at 9:42
  • I have corrected the and now I am getting the correct category url but it isn't redirecting to that url. @YiorgosMoschovitis
    – zeusukdm
    Commented Dec 10, 2015 at 9:50
  • I have created the custom category attribute which is passcode and the value is coming from $customerclass = $session->getPasscode(); @Abdul
    – zeusukdm
    Commented Dec 10, 2015 at 9:50
  • Do you get any category selection from customer while registering?
    – Mujahidh
    Commented Dec 10, 2015 at 11:18

1 Answer 1

2

Try the following code. I assume (by your code) that you want the first category if there are many.

$categories = Mage::getModel('catalog/category')
          ->getCollection()
          ->addAttributeToSelect('*')
          ->addAttributeToFilter('custom_attribute', 'value')
          ->addIsActiveFilter()
          ->addUrlRewriteToResult();

if($categories->count()){
     $url = $categories->getFirstItem()->getUrl();
     Mage::app()->getFrontController()->getResponse()->setRedirect($url)->sendResponse();
     exit;
}

I have to warn you that it's never a good idea to put redirects in template files.

-- Edit --

Try this

if($session = Mage::getSingleton('customer/session')){
     $customerclass = $session->getPasscode();
     $referer = Mage::app()->getRequest()->getServer('HTTP_REFERER');

     if( $referer == 'http://www.localhost.com/customer/account/create/' && $customerclass != ''){

         $categories = Mage::getModel('catalog/category')
              ->getCollection()
              ->addAttributeToSelect('*')
              ->addAttributeToFilter('passcode', $customerclass)
              ->addIsActiveFilter()
              ->addUrlRewriteToResult();

         if($categories->count()){
              $url = $categories->getFirstItem()->getUrl();
              if ($url != $referer) { //overkill yeah
                   Mage::app()->getFrontController()->getResponse()->setRedirect($url)->sendResponse();
                   exit;
              }
         }
     }
}
4
  • It worked however the browser first gives Error too many redirect loop and then it redirects to the category page. Is that normal? @YiorgosMoschovitis
    – zeusukdm
    Commented Dec 10, 2015 at 10:22
  • Well... I told you about redirects in phtml files. Never a good idea. Did you include your refferer check with my code? It seems like you haven't Commented Dec 10, 2015 at 10:25
  • Also thank you for your warning but I couldn't get it working in any other way. I have tried using event hook but it didn't work. You can see it here magento.stackexchange.com/questions/92956/… Do you suggest a different way? if yes how? @YiorgosMoschoviti
    – zeusukdm
    Commented Dec 10, 2015 at 10:27
  • I have updated the code you can have a look at it @YiorgosMoschovitis
    – zeusukdm
    Commented Dec 10, 2015 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.