James S
James S

Reputation: 31

Magento New Products Filter Attribute

I have followed this guide, and have had new products display for some time now, but the filter on the left hand side never displayed: http://www.dnawebagency.com/displaying-new-products-in-magento-with-pagination/

This code is as follows under /app/code/local/Mage/Catalog/Block/Product/New.php

<?php

class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_List
{
   function get_prod_count()
   {
      //unset any saved limits
      Mage::getSingleton('catalog/session')->unsLimitPage();
      return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12;
   }// get_prod_count

   function get_cur_page()
   {
      return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1;
   }// get_cur_page

   /**
    * Retrieve loaded category collection
    *
    * @return Mage_Eav_Model_Entity_Collection_Abstract
   **/
   protected function _getProductCollection()
   {
      $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

      //$collection = Mage::getResourceModel('catalog/product_collection');
    $collection = Mage::getModel('catalog/product')->getCollection();
      $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

      $collection = $this->_addProductAttributesAndPrices($collection)
         ->addStoreFilter()
         ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
         ->addAttributeToFilter('news_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
         ), 'left')
         ->addAttributeToSort('news_from_date', 'desc')
         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());

      $this->setProductCollection($collection);

      return $collection;
   }// _getProductCollection
}// Mage_Catalog_Block_Product_New
?>

Now I got the filter on the page using the following XML on the page template

<!-- add layered navigation to left column -->
<reference name="left">
  <block type="catalog/layer_view" name="catalog.leftnav" before="-"  template="catalog/layer/view.phtml" />
</reference>

Now the filter on the left hand side displays, but it shows all category counts, and when clicking any of the filters, it does not narrow the search down on the current new stock.

I have been scratching my head and searching all over the net to try and get this to work.

Anyone have any ideas?

The full xml on the page is as follows

<action method="addFilter">
</action>
<reference name="content">
    <block type="catalog/product_new" name="product_list" after="product_filter"  template="catalog/product/list.phtml">
      <action method="setCategoryId"><category_id>3</category_id></action>
      <block type="catalog/product_list_toolbar" name="product_list_toolbar"   template="catalog/product/list/toolbar.phtml">
        <block type="page/html_pager" name="product_list_toolbar_pager"/>
      </block>
      <action method="addColumnCountLayoutDepend"><layout>empty</layout>
          <count>6</count>
      </action>
      <action method="addColumnCountLayoutDepend">
          <layout>one_column</layout>
          <count>5</count>
      </action>
      <action method="addColumnCountLayoutDepend">
          <layout>two_columns_left</layout>
          <count>3</count>
      </action>
      <action method="addColumnCountLayoutDepend">
          <layout>two_columns_right</layout>
          <count>4</count>
      </action>
      <action method="addColumnCountLayoutDepend">
          <layout>three_columns</layout>
          <count>3</count>
      </action>
      <action method="setToolbarBlockName">
          <name>product_list_toolbar</name>
      </action>
    </block>
</reference>

<!-- add layered navigation to left column -->
<reference name="left">
  <block type="catalog/layer_view" name="catalog.leftnav" before="-"  template="catalog/layer/view.phtml" />
</reference>

Upvotes: 3

Views: 4068

Answers (2)

Jagi Yadav
Jagi Yadav

Reputation: 111

Call a block on home page through static block with content value as

{{block type="catalog/product_list" name="product_listing" template="catalog/product/new.phtml"}}

or you can directly call this block on cms home , paste this

{{block type="catalog/product_list" name="product_listing" template="catalog/product/new.phtml" }}

in your CMS home page content.

create a file in folder frontend/theme_folder/default/template/catalog/product as new.phtml.

Paste the below code in it as :

New Products

<?php
$_productCollection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->setVisibility(array(2,3,4))                   
                    ->setOrder('created_at', 'desc')
                    ->setPage(1, 4);
?>
 <?php $_iterator = 0; ?>
     <ul class="products-grid">
            <?php  foreach($_productCollection as $_product) : ?>
            <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
                <?php // Product Image ?>
                       <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>

                      <div class="product-shop">
                            <div class="f-fix">
                                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></h2>

                                 <?php echo $this->getPriceHtml($_product, true) ?>
                                <?php if($_product->isSaleable()): ?>
                                    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                                <?php else: ?>
                                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                                <?php endif; ?>
                            </div>
                    </div>
              </li>
        <?php endforeach;  ?>
    </ul>

Thats it you will be able to see the newest 4 product on home page. you can increase the number of product by increasing the ->setPage(1, 4); to your required number of product on home page.

Upvotes: 1

Amit Bera
Amit Bera

Reputation: 7611

Please specify the category id for layer

 <reference name="left">
        <!-- Layered Navigation Block -->
        <block type="catalog/layer_view" name="catalog.leftnav" template="catalog/layer/view.phtml" >
                <action method="setCategoryId"><category_id>3</category_id></action>
        </block>
</reference>

More details please check....

CMS page with layered navigation not working

Upvotes: 1

Related Questions