Question

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;
        }
    }
}
Was it helpful?

Solution

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;
              }
         }
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top