1

I am trying to forward every category page to a product view page using an event observer. But it seems to be not working.

Scenario : I am playing around with Magento router now. As part of this, I am trying to forward every category page in my Magento instance to the product page. The product page which is going to load for a category is the page of first product in that category.

What I have Tried : I observed to the event controller_action_predispatch_catalog_category_view and added below code to my observer method.

public function forwardSingleProCategory(Varien_Event_Observer $observer)
{
    //get event data
    $action = $observer->getControllerAction();
    $request = $action->getRequest();

    //get category id
    $categoryId = (int)$request->getParam('id', false);

    //grab category collection
    $layer = Mage::getModel('catalog/layer')->setCurrentCategory($categoryId);
    $collection = $layer->getProductCollection();

    //check whether category count is 1. If YES, then do magic
    if ($collection->getSize() > 0) {

        //prepare parameters that needs for the action `catalog/product/view`
        $product = $collection->getFirstItem();
        $actionName = 'view';
        $controllerName = 'product';
        $moduleName = 'catalog';
        $params = array(
            'category' => $categoryId,
            'id'       => $product->getId()
        );

        //tells no to category page router processing further
        $request->setDispatched(false);

        //throw exception with a clear message we need a product page.
        $e = new Mage_Core_Controller_Varien_Exception();
        $e->prepareForward($actionName, $controllerName, $moduleName, $params);
        throw $e;

    }

    return $this;
}

See the comments that I have put in the code. If something is not clear in my code please let me know. I can explain

But I am getting 404 Page for every category page request. Ooops !!!

How much I debug : The 404 page is triggering from Mage_Catalog_ProductController itself (which means observer is working fine). The problem lies here in this section :

 public function viewAction()
{
    // Get initial data from request
    $categoryId = (int) $this->getRequest()->getParam('category', false);
    $productId  = (int) $this->getRequest()->getParam('id');
    $specifyOptions = $this->getRequest()->getParam('options');
    ...
    try {
        $viewHelper->prepareAndRender($productId, $this, $params);
    } catch (Exception $e) {
        if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
            if (isset($_GET['store'])  && !$this->getResponse()->isRedirect()) {
                $this->_redirect('');
            } elseif (!$this->getResponse()->isRedirect()) {
                $this->_forward('noRoute');
            }
        } else {
            Mage::logException($e);
            $this->_forward('noRoute');
        }
    }

The problem is $productId and $categoryId are same (seems that both holds category id value) and hence the line $viewHelper->prepareAndRender($productId, $this, $params); is failing and thus I am getting 404 page.

But in my observer, you can see that I am setting both category and id parameters via this section

$e = new Mage_Core_Controller_Varien_Exception();
$e->prepareForward($actionName, $controllerName, $moduleName, $params);

I double checked and confirmed that $params (holds the right product id for id and right category id for category) are correct in my observer.

So my question is how it is get rewriting by category Id in the router process in this scenario ?

How can I make it work ?

Rajeev K Tomy
  • 2,206
  • 1
  • 18
  • 35

0 Answers0