Magento 1.8 – How to Get New Products from Each Category on Home Page

categoryce-1.8.1.0cmshomemagento-1.8

What I got: I have Magento CE 1.8.1.0 installed

What I need: I want to show a few recent products for each category in my home, like they've done in Instacart's home.
Instacart store home

How do I picture the solution: This view is way similar to the one generated by the New Product List, however the New Product List do not accept category filtering. Therefore, I'd want to edit the New.php file to accept category filtering.

What I have tried: I have tried to edit a copy of New.php in the [app/code/local/Mage/Catalog/Block/Product] directory, to alter the _beforeHtml function, as showed in this StackOverflow question, as well as updating both _beforeHtml and getCacheKeyInfo while creating the new getCategory support function as proposed in this StackExchange question. Neither seems to work for me, changes in the getCacheKeyInfo makes the block disappear altogheter, while other changes ignore the category_id filter in the block call.

Best Answer

Create a local.xml under app/design/frontend/yourpackage/yourtemplate/layout/local.xml code of this file are

< ?xml version="1.0"?>
<layout version="0.1.0">
 <cms_index_index>

       <reference name="content">
            <block type="core/template" name="my_default_home_page" template="cms/home.phtml"/>
        </reference>
    </cms_index_index>

</layout>

and create home.phtml under app/design/frontend/yourpackage/yourtemplate/template/cms and code is this file below

here the code

$catgorcollection=Mage:getModel('catalog/category')->getCollection();
foreach($catgorcollection as $category ){

$todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection = Mage::getResourceModel('catalog/product_collection'); 
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
    $collection
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addUrlRewrite();
$collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('news_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter(
                array(
                    array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                    )
              )
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize(7)
            ->setCurPage(1);

$collection->addCategoryFilter($category);
}