1

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.

2 Answers 2

2

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);
}
0

first copy new.php file from app\code\core\Mage\Catalog\Block\Product\New.php to app\code\local\Mage\Catalog\Block\Product\New.php

now edit this file near line 88

protected function _getProductCollection()
{
...............
// put  here code line is near 122 
$category = Mage::getModel('catalog/category')->load($this->getData('catagory_id'));
$collection->addCategoryFilter($category);
// code end
return $collection;
}

now add block in home page cms {{block type="catalog/product_new" catagory_id="28" template="catalog/product/new.phtml"}} here 28 is my category id put multiple blcok like above and chanege catagory_id for diffent categories

please vote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.