Php – How to show NEW products per row from specific categories in Magento

categoriesdatabasee-commercemagentophp

As the title suggests, I like to show new products called in from specific categories separated by rows in the home page.

Row 1 -> New products from Category 1

Row 2 -> New products from Category 2

Row 3 -> New products from Category 3

…etc

In my Magento Admin CMS, under Home Page they will be called in as separate blocks:

<block type="catalog/product_new" name="home.catalog.product.new_category_1" alias="product_new_category_1" template="catalog/product/new_category_1.phtml">

<block type="catalog/product_new" name="home.catalog.product.new_category_2" alias="product_new_category_2" template="catalog/product/new_category_2.phtml">

<block type="catalog/product_new" name="home.catalog.product.new_category_3" alias="product_new_category_3" template="catalog/product/new_category_3.phtml">

...etc

Basically I’m thinking of duplicating new.phtml and calling it new_category_1.phtml, new_category_2.phtml, etc and get “new” products from category id 1, category id 2 respectively.

I played with Mage::getModel('catalog/category')->getCollection();, getProductCollection and getCatId and can’t get it working in a copy of new.phml (app/design/frontend/default/default/template/catalog/product/).

The code below works but does not load "new" assigned products within category id assigned, it loads all products within it.

<?php 
                       
$cat_id = 46; // category id
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');

if (($products=($_products = $this->getProductCollection()) && $_products->getSize())): ?>

<div class="hp-report">
    <div class="head-alt">
        <h2 class="title"><?php echo $this->__('New Products') ?></h2>
    </div>
    <table cellspacing="0" class="generic-product-grid" id="new-products-list-table">
        <tr>
        <?php $i=0; foreach ($_products->getItems() as $_product): ?>
            <?php if ($i>=4): continue; endif; ?>
        
            <td>
                <p class="product-image">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170) ?>" width="170" height="170" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                </p>
                <p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php echo $this->getPriceHtml($_product, true, '-new') ?>
                <?php if($_product->isSaleable()): ?>
                    <a href="<?php echo $this->getAddToCartUrl($_product) ?>"><img src="<?php echo $this->getSkinUrl('images/btn_add_to_cart.gif') ?>" alt="<?php echo $this->__('Add to Cart') ?>" title="<?php echo $this->__('Add to Cart') ?>" /></a>
                <?php else: ?>
                <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
                <?php endif; ?>
                <div class="clear"></div>
                <ul class="add-to">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-cart"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
                </td>
        <?php $i++; endforeach; ?>
        <?php for($i;$i%4!=0;$i++): ?>
                <td>&nbsp;</td>
            <?php endfor ?>
            </tr>
    </table>
    <script type="text/javascript">decorateTable('new-products-list-table');</script>
</div>

<?php endif; ?>

Any thoughts appreciated.

Best Solution

Th following collection query should get you what you want

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);            
$products = $category->
getProductCollection()->
addCategoryFilter($category)->
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')->            
addAttributeToSelect('*');

A products "newness" is determined by two attributes, news_from_date and news_to_date, so you want to add two additional attributes to the filter. The specific method calls from above that do this are

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')->            

They're taken directly from the New Product block at

/app/code/core/Mage/Catalog/Block/Product/New.php
Related Question