1

I have setup up a custom list view with new products based on the products list view.

<block type="catalog/product_specials" name="product_new" template="catalog/product/list.phtml">

My problem is now the layered navigation do not appear in the left sidebar as on the default list view.

Is it possible to integrate layered navigation in a custom list view?

Specials.php:

/**
 * Product Collection
 *
 * @var Mage_Eav_Model_Entity_Collection_Abstract
 */
protected $_productCollection;

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
        }

        // if this is a product view page
        if (Mage::registry('product')) {
            // get collection of categories this product is associated with
            $categories = Mage::registry('product')->getCategoryCollection()
                ->setPage(1, 1)
                ->load();
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category
                $this->setCategoryId(current($categories->getIterator()));
            }
        }

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $this->_productCollection = $layer->getProductCollection();

    $dateToday = date('m/d/y');
    $tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
    $dateTomorrow = date('m/d/y', $tomorrow);

    $this->_productCollection
     ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $dateToday))
     ->addAttributeToFilter('special_to_date', array('or'=> array(
        0 => array('date' => true, 'from' => $dateTomorrow),
        1 => array('is' => new Zend_Db_Expr('null')))
         ), 'left');
        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }
    }

    return $this->_productCollection;
}

/**
 * Get catalog layer model
 *
 * @return Mage_Catalog_Model_Layer
 */
public function getLayer()
{
    $layer = Mage::registry('current_layer');
    if ($layer) {
        return $layer;
    }
    return Mage::getSingleton('catalog/layer');
}

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
public function getLoadedProductCollection()
{
    return $this->_getProductCollection();
}

/**
 * Retrieve current view mode
 *
 * @return string
 */
public function getMode()
{
    return $this->getChild('toolbar')->getCurrentMode();
}

/**
 * Need use as _prepareLayout - but problem in declaring collection from
 * another block (was problem with search result)
 */
protected function _beforeToHtml()
{
    $toolbar = $this->getToolbarBlock();

    // called prepare sortable parameters
    $collection = $this->_getProductCollection();

    // use sortable parameters
    if ($orders = $this->getAvailableOrders()) {
        $toolbar->setAvailableOrders($orders);
    }
    if ($sort = $this->getSortBy()) {
        $toolbar->setDefaultOrder($sort);
    }
    if ($dir = $this->getDefaultDirection()) {
        $toolbar->setDefaultDirection($dir);
    }
    if ($modes = $this->getModes()) {
        $toolbar->setModes($modes);
    }

    // set collection to toolbar and apply sort
    $toolbar->setCollection($collection);

    $this->setChild('toolbar', $toolbar);
    Mage::dispatchEvent('catalog_block_product_list_collection', array(
        'collection' => $this->_getProductCollection()
    ));

    $this->_getProductCollection()->load();

    return parent::_beforeToHtml();
}

/**
 * Retrieve Toolbar block
 *
 * @return Mage_Catalog_Block_Product_List_Toolbar
 */
public function getToolbarBlock()
{
    if ($blockName = $this->getToolbarBlockName()) {
        if ($block = $this->getLayout()->getBlock($blockName)) {
            return $block;
        }
    }
    $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
    return $block;
}

/**
 * Retrieve additional blocks html
 *
 * @return string
 */
public function getAdditionalHtml()
{
    return $this->getChildHtml('additional');
}

/**
 * Retrieve list toolbar HTML
 *
 * @return string
 */
public function getToolbarHtml()
{
    return $this->getChildHtml('toolbar');
}

public function setCollection($collection)
{
    $this->_productCollection = $collection;
    return $this;
}

public function addAttribute($code)
{
    $this->_getProductCollection()->addAttributeToSelect($code);
    return $this;
}

public function getPriceBlockTemplate()
{
    return $this->_getData('price_block_template');
}

/**
 * Retrieve Catalog Config object
 *
 * @return Mage_Catalog_Model_Config
 */
protected function _getConfig()
{
    return Mage::getSingleton('catalog/config');
}

/**
 * Prepare Sort By fields from Category Data
 *
 * @param Mage_Catalog_Model_Category $category
 * @return Mage_Catalog_Block_Product_List
 */
public function prepareSortableFieldsByCategory($category) {
    if (!$this->getAvailableOrders()) {
        $this->setAvailableOrders($category->getAvailableSortByOptions());
    }
    $availableOrders = $this->getAvailableOrders();
    if (!$this->getSortBy()) {
        if ($categorySortBy = $category->getDefaultSortBy()) {
            if (!$availableOrders) {
                $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
            }
            if (isset($availableOrders[$categorySortBy])) {
                $this->setSortBy($categorySortBy);
            }
        }
    }

    return $this;
}

}

0

1 Answer 1

0

To add layered navigation to a custom page you basically need

1) Have the navigation block in the page layout
2) Use the Catalog Layer object to load product collection (and the modify it if needed)
3) Adjust the options URLs to point to your custom page

1
  • Have you implemented this? Commented Nov 23, 2018 at 9:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.