0

I am trying to redirect customer to a specific category based on the entered code. Everything works fine except the redirect. Magento redirects to homepage instead of category page. How can I redirect to category page? Any can help?

Customer Registration Form extra field to get the correct category:

<input type="text" name="login[passcode]" id="passcode" value="" title="<?php echo $this->__('Passcode') ?>" class="input-text required-entry" />

My config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Team_Savepasscode>
            <version>0.0.1</version>
        </Team_Savepasscode>
    </modules>
    <global>
        <models>
            <team_savepasscode>
                <class>Team_Savepasscode_Model</class>
            </team_savepasscode>
        </models>
        <events>
            <customer_register_success>
                <observers>
                    <team_savepasscode>
                        <class>team_savepasscode/observer</class>
                        <method>customerRegisterSuccess</method>
                        <type>singleton</type>
                    </team_savepasscode>
                </observers>
            </customer_register_success>
        </events>
    </global>
</config>

Customer registration success event observer:

public function customerRegisterSuccess(Varien_Event_Observer $observer) {
    $classcode = Mage::app()->getRequest()->getPost('login');
    $session = Mage::getSingleton('customer/session');
    $session->setPasscode($classcode);

    $categories = Mage::getResourceModel('catalog/category_collection');
    $categories->addAttributeToSelect('*');
    $categories->addAttributeToFilter('is_active', 1);
    $categories->addAttributeToFilter('passcode', $classcode);
    $categories->addUrlRewriteToResult();
    $categories->setCurPage(1)->setPageSize(1);
    $categories->load();

    if ($categories->getFirstItem()) {
        $category = $categories->getFirstItem();
        $successUrl = $session->getBeforeAuthUrl(true);
        $successUrl = Mage::getUrl($category->getURL($category), array('_secure' => true));
        return $successUrl;
    }
    else {
        echo 'No category exists with the name ' . $name;
    }

}
1

As you want to redirect to a category,so u can use magento FrontController redirction action from any view.

 Mage::app()->getResponse()->setRedirect($category->getUrl($category));
 Mage::app()->getResponse()->sendResponse();
exit();

Place this code on place of

$successUrl = Mage::getUrl($category->getURL($category), array('_secure' => true));

  • Hi, where should I put your code? @Amit Bera – tufanayd Dec 8 '15 at 7:58
  • check the uoate – Amit Bera Dec 8 '15 at 8:03
  • I have replaced it but redirecting to homepage @Amit Bera – tufanayd Dec 8 '15 at 10:30
  • one problem, if I remove exit(); it redirects to homepage, customer logins, however with the exit(); it redirects to the category page which is perfect BUT the customer doesn't login which is big problem. how can I redirect to the category page and the customer logins? @Amit Bera – tufanayd Dec 8 '15 at 16:15
0

Add bellow code in your observer file:

public function customerRegisterSuccess(Varien_Event_Observer $observer) 
    {
        $classcode = Mage::app()->getRequest()->getPost('login');
        $session = Mage::getSingleton('customer/session');
        $session->setPasscode($classcode);

        $categories = Mage::getResourceModel('catalog/category_collection');
        $categories->addAttributeToSelect('*');
        $categories->addAttributeToFilter('is_active', 1);
        $categories->addAttributeToFilter('passcode', $classcode);
        $categories->addUrlRewriteToResult();
        $categories->setCurPage(1)->setPageSize(1);
        $categories->load();

        if ($category = $categories->getFirstItem()) {
           $successUrl = Mage::getUrl($this->getCategoryUrl($category), array('_secure' => true));
            return successUrl;
        }
        else {
            echo 'No category exists with the name ' . $name;
        }
    }

    public function getCategoryUrl($category)
    {
        if ($category instanceof Mage_Catalog_Model_Category) {
            $url = $category->getUrl();
        } else {
            $url = $this->_getCategoryInstance()
                ->setData($category->getData())
                ->getUrl();
        }

        return $url;
    }
  • Hi I tried your code and get the following error Call to a member function getResponse() on a non-object in @Abdul – tufanayd Dec 8 '15 at 7:52
  • updated ans pls check now – Abdul Dec 8 '15 at 7:58
  • Not working ir redirects to /customer/account/createpost/ with a complete blank page @Abdul – tufanayd Dec 8 '15 at 8:05
0

you have to setRedirect from observer.

public function customerRegisterSuccess(Varien_Event_Observer $observer) {
    $classcode = Mage::app()->getRequest()->getPost('login');
    $session = Mage::getSingleton('customer/session');
    $session->setPasscode($classcode);

    $categories = Mage::getResourceModel('catalog/category_collection');
    $categories->addAttributeToSelect('*');
    $categories->addAttributeToFilter('is_active', 1);
    $categories->addAttributeToFilter('passcode', $classcode);
    $categories->addUrlRewriteToResult();
    $categories->setCurPage(1)->setPageSize(1);
    $categories->load();
    foreach($categories $as $category){
       $categories = Mage::getModel('catalog/category')->load($category->getId());
    }


    if ($categories->getFirstItem()) {
        $category = $categories->getFirstItem();
        $successUrl = $session->getBeforeAuthUrl(true);
        $successUrl = Mage::getUrl($category->getURL($category), array('_secure' => true));

    Mage::app()->getResponse()->setRedirect($successUrl);
    Mage::app()->getResponse()->sendResponse();
    exit;
    }
    else {
        echo 'No category exists with the name ' . $name;
    }

}
  • Not working, still redirects to the homepage @Qaisar Satti – tufanayd Dec 8 '15 at 8:05
  • updated kindly check.. – Qaisar Satti Dec 8 '15 at 9:06
  • Redirecting to homepage instead of category page @Qaisar Satti – tufanayd Dec 8 '15 at 10:23
  • remove $successUrl = $session->getBeforeAuthUrl(true); this and check – Qaisar Satti Dec 8 '15 at 10:31
  • still no change, redirecting to homepage @Qaisar Satti – tufanayd Dec 8 '15 at 10:35

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.