views:

176

answers:

2

Hi all

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

A: 

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

Joseph Mastey
Could you be more specific? As a programmer, I understand what you mean when you talk about override _beforeToHtml method but I’m not a php programmer and my knowledge of this language is limited. I guess that the file I should modify is the Core file app\code\core\Mage\Catalog\Block\Product\New.php, isn’t it? Below is my code, can you help me to complete it?Thanks for your help, J.
juankomik
A: 

Could you be more specific? As a programmer, I understand what you mean when you talk about override _beforeToHtml method but I’m not a php programmer and my knowledge of this language is limited.

I guess that the file I should modify is the Core file app\code\core\Mage\Catalog\Block\Product\New.php, isn’t it?

Here is my code,

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

    $collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $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)
    ;
    $this->setProductCollection($collection);

    return parent::_beforeToHtml();
}

Can you help me to complete the code?

Thanks for your help, J.

juankomik