开发者

New products by category in Magento

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_homep开发者_Python百科age" 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


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...


The catalog/product_new block doesn't accept a category_id as a parameter. If you want to do this for a single category, you'll need to create your own custom block that descends from catalog/product_new and override the method _beforeToHtml to utilize a category_id.

Hope that helps!

Thanks, Joe


I think that you want to alter the New.php like this:

$collection = Mage::getResourceModel('catalog/product_collection');

becomes this (getStoreId optional)...

$featCat = Mage::getModel('catalog/category')->load($this->getCategory()); $collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($this->getStoreId())->addCategoryFilter($featCat);

...obviously you will have to create getter and setter like 'setCategory', 'getCategory'


This works for me.

    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->getCategoryId()){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
}

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜