Link Top Menu Item to Product View

Updated . Posted . Visible to the public.

Top menu is configured in backend > Catalog > Manage Categories. The landing page can be set to product list, static block, or a combination of both. But what if we want to by pass the product listing and land on a product page? Here's the technique I use to do just that:

[vs code] Create a Couple of Blocks

/**
 * es Module
 *
 * @category   Celera
 * @package    Celera_Es
 * @copyright  Copyright (c) 2019 Ng Kiat Siong, Celera eShop, kiatsiong.ng@gmail.com  
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

class Celera_Es_Block_Page_Static_ProductView extends Celera_Es_Block_Page_Static_Redirect
{
    /**
     * Redirect static block to product page
     */
    protected function _construct()
    {
        $this->_urlPath = 'catalog/product/view';
        $sku = $this->getSku();
        $pid = Mage::getResourceSingleton('catalog/product')->getIdBySku($sku);
        if ($pid) {
            $this->_urlParams = array(
                'id' => $pid,
                'category' => Mage::registry('current_category') ? Mage::registry('current_category')->getId() : false
            );
        }
        return parent::_construct();
    }
}
/**
 * es Module
 *
 * @category   Celera
 * @package    Celera_Es
 * @copyright  Copyright (c) 2019 Ng Kiat Siong, Celera eShop, kiatsiong.ng@gmail.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

class Celera_Es_Block_Page_Static_Redirect extends Mage_Core_Block_Template
{
    protected $_urlParams = array();
    protected $_urlPath = '';

    /**
     * Redirect static block to the set URL
     */
    protected function _construct()
    {
        if (!$this->_urlPath) {
            $this->_urlPath = $this->getUrlPath();
        }
        if ($this->_urlPath) {
            $url = $this->getUrl($this->_urlPath, $this->_urlParams);
            Mage::app()->getResponse()->setRedirect($url, 301);
        }
        return parent::_construct();
    }
}

[Backend] Whitelist the Block es/page_static_productView

Backend > System > Permissions > Blocks:

Image

[Backend] Create a Static Block

Backend > CMS > Manage Static Blocks > Add New Block:
With the content:

Product View isac{{block type="es/page_static_productView" sku="isac"}}

where es is the name of my module.

Image

[Backend] Set Menu Item

Backend > Catalog > Manage Categories > select the category (menu item) > Display Settings

Set Display Mode to Static block only
Set CMS Block to Product View isac

Image

Refresh the cache and that's it!

Mage Ver 1.9.2.0

After ver 1.9.2.0, the above will not work when cache block is enabled. To fix it, we need to overwrite Mage_Cms_Block_Block:

/**
 * Fix not rendering dynamic content when cache is enable
 * @see Mage_Catalog_Block_Category_View ::getCmsBlockHtml()
 * 
 * @param int $id
 * @return $this
 */
public function setBlockId($id)
{
    $this->setData('block_id', $id);
    $block = Mage::getModel('cms/block')->load($id);
    if (strpos($block->getContent(), '{{block') !== false) {
        $this->unsCacheLifetime();
    }
    return $this;
}
kiatng
Last edit
kiatng
Posted by kiatng to OpenMage (2019-07-29 01:13)