Magento – New products by category in Magento

categoriesmagento

This should be supposed to be silly thing but it’s driving me nuts!

All I want is to show the new products for a specified category, but all I get is new products from any category.

Supposing I want to show up the category 74, I’ve tried almost any combination of that code

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" category_id="74" template="catalog/product/new.phtml"}}

With a category_id directly assigned

{{block type="catalog/product_new" category_id="74" template="catalog/product/new.phtml"}} 

With category implicit depending or which category I’m accessing

{{block type="catalog/product_new" template="catalog/product/new.phtml"}}

This code has been tested, hardcoded in home page code, creating a static block with it and including it through category panel (static block and products)…but worthless. I’ve tested almost any code I’ve found in this and other forums but no result.

Regardless of any code (with category_id="74" assigned and not), I’m all the time getting the same result. The category filter doesn’t work and shows me new products from any category, not the new products of the current category, neither the products of a handcoded category (for instance category_id="74")

On the other hand if I use list products instead of new products, works OK in home page and as static block, so categories seem to be well created

{{block type="catalog/product_list" category_id="74" template="catalog/product/list.phtml"}}

That shows me products that belong to category 74

I’m using magento Magento ver. 1.3.2.4, and shows me the same result even using different templates.

Any advice will be welcomed

Have a nice day, J.

PS I've tried also with other categories, not only 74. With parent categories, child categories…but no result at all

Best Answer

Finally very simple...

Use the definition block below :

{{block type="catalog/product_new" name="home.catalog.product.new" alias="allCatalogNewProducts" category_id="5" template="catalog/product/new.phtml"}}

To retrieve the category_id, you should modifiy the _beforeToHtml function of the new class like this :

File in \app\code\core\Mage\Catalog\Block\Product\New.php Prefer surcharge this file in local path

   protected function _beforeToHtml()
    {
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection = Mage::getResourceModel('catalog/product_collection');
        $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->getProductsCount())
            ->setCurPage(1)
        ;

      if($categoryId=$this->getData('category_id')){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
      }


        $this->setProductCollection($collection);

        return parent::_beforeToHtml();

You can see that an if statement has been added :

  if($categoryId=$this->getData('category_id')){
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $collection->addCategoryFilter($category);
  }

You retrieve the category_id with the function $this->getData('category_id')

Enjoy...

Related Topic