Magento – Apply Layered Navigation to CMS Page with New Products

categorycollection;filter

Can't seem to get it to show up.

This is how it's defined in layout files:

<block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>

it's located here:

/app/design/frontend/name-space/default/template/catalog/layer/view.phtml

I'm attempting to add it to the CMS page via the Layout Update XML (Layout is 2 columns with left bar) section in the backend using:

<reference name="content">
    <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"></block>
</reference>

I've also tried:

<reference name="content">
    <block type="enterprise_search/catalog_layer_view" name="catalog.content" before="-" template="catalog/layer/view.phtml">
</reference>

The former shows nothing at all and the latter shows a single category dropdown that redirects to that specific category page.

Also, the product collection on the CMS page is being loaded by a widget. Would this have some effect? In the CMS page Content section is found:

{{widget type="catalog/product_widget_new" display_type="new_products" show_pager="1" products_count="60" template="catalog/product/widget/new/content/new_grid.phtml"}}

The above widget calls a function called _getProductCollection() in order to generate the product Collection of new products:

protected function _getProductCollection()
{
    $todayStartOfDayDate = Mage::app()->getLocale()->date()
        ->setTime('00:00:00')
        ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    $todayEndOfDayDate = Mage::app()->getLocale()->date()
        ->setTime('23:59:59')
        ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    /** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
    $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
    $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToFilter('news_from_date', array('or' => array(
            0 => array('date' => true, 'to' => $todayEndOfDayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToFilter('news_to_date', array('or' => array(
            0 => array('date' => true, 'from' => $todayStartOfDayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToFilter(
            array(
                array('attribute' => 'news_from_date', 'is' => new Zend_Db_Expr('not null')),
                array('attribute' => 'news_to_date', 'is' => new Zend_Db_Expr('not null'))
            )
        )
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1);
    return $collection;
}

Best Answer

Layered navigation needs an active category to be able to work. If I remember correctly this should do it.

<block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml">
   <action method="setCategoryId"><category_id>[category ID]</category_id></action>
</block>

As a side note, there is a free module that gives you pages with products and a layered navigation and some content fields like CMS pages.

https://github.com/magento-hackathon/layered-landing

-- EDIT

To have a category with new products create a custom module that runs a cron every night that assigns the products to the new category. Below will add all products added after last week.

[Namespace]_[Module]_Model_Cron 
{
    public function assignToNew()
    {
        $collection = Mage::getResourceModel('catalog/product_collection')
        $collection->addAttributeToFilter('updated_at', array('gteq' => date('Y-m-d', strtotime('-1 week')));
        $collection->addAttributeToFilter('status', 1);
        [...]
        // whatever you need to filter
        [...]

        $products = array_fill_keys($collection->getAllIds(), '1');

        $category = Mage::getModel('catalog/category')->load([id of the cat]);
        $category->setPostedProducts($products);

        $category->save();
    }
}
Related Topic