Magento E-commerce Platform Q/ A

Custom Redirect from a Category Page in Magento

Let's assume your customer needs to redirect store users when they click on some category link to some particular product page. You can do it by creating a simple module. 

The structure of the etc/config.xml file:

<config>
	<modules>
		<Ikantam_Redirect>
			<version>1.0.0.0</version>
		</Ikantam_Redirect>
	</modules>
	<frontend>
<routers>
            		<ikantam_redirect>
               			 <use>standard</use>
                			<args>
                    				<module>Ikantam_Redirect</module>
                   				<frontName>redirect</frontName>
                			</args>
            		</ikantam_redirect>
        		</routers>
	</frontend>
	<global>
		<rewrite>
			<ikantam_redirect_category>
				<from><![CDATA[#^/catalog/category/#]]></from>
				<to>/redirect/category/</to>
			</ikantam_redirect_category>
		</rewrite>
	</global>
</config>

 

Using the above code we've rewritten default Mage_Catalog_CategoryController controller to Ikantam_Redirect_CategoryController.

Now we will add an additional parameter to the category

Code: sql/redirect_setup/install-1.0.0.0.php:

<?php

$installer = $this;
$installer->startSetup();
/** adding category attribute */
$installer->addAttribute('catalog_category', 'redirect_category_source', array(
    'group'         => 'General Information',
    'type'          => 'varchar',
    'label'         => 'Custom redirect to',
    'input'         => 'text',
    'required'      => false,
    'sort_order'    => 90,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE
));
$installer->endSetup();

 

Thus we have created additional parameter: 'redirect_category_source’ in 'General Information' tab titled 'Custom redirect to'. See the screenshot:

237

With the help of this parameter the site admin will enable a custom redirect from this category by entering the URL and saving the configuration.

Also we need to create the controller that will be checking presence of the added parameter.

Code: CategoryController.php:

<?php
class Ikantam_Redirect_CategoryController extends Mage_Core_Controller_Front_Action
{
protected function _initCatagory()
    {		
        Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
        $categoryId = (int) $this->getRequest()->getParam('id', false);
        if (!$categoryId) {
            return false;
        }

        $category = Mage::getModel('catalog/category')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($categoryId);

        if (!Mage::helper('catalog/category')->canShow($category)) {
            return false;
        }
        Mage::getSingleton('catalog/session')->setLastVisitedCategoryId($category->getId());
        Mage::register('current_category', $category);
		
/*Redirect code*/
	$url = $category->getRedirectCategorySource();
	if (!empty($url)) {
		$url = $category->getRedirectCategorySource();
        		Mage::app()->getResponse()->setRedirect($url)->sendResponse();
	}
        try {
            Mage::dispatchEvent(
                'catalog_controller_category_init_after',
                array(
                    'category' => $category,
                    'controller_action' => $this
                )
            );
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
            return false;
        }
        return $category;
    }
}

That's it.