Thursday 5 June 2014

How to display New products in Magento?

Hello Magento lovers!, have you ever wanted to display New products in your store by just setting the product “Set Product as New from Date” within the product configuration in your Admin-panel?. It simple, luckily Magento provides php functions to do almost anything you wish, in this case we’ll use the Mage::getResourceModel(‘catalog/product_collection’) features:
We’ll create a template file called: new_products.phtml
The content would be:


<?php
$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
 
$_productCollection=$this->getLoadedProductCollection();
$_productCollection = Mage::getResourceModel(‘catalog/product_collection’)
->addAttributeToSelect(‘*’)
->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’);
 
$now = date(“Y-m-d”);
$newsFrom= substr($_productCollection->getData(‘news_from_date’),0,10);
$newsTo=  substr($_productCollection->getData(‘news_to_date’),0,10);
 
?>
 
<?php if(!$_productCollection->count()): ?>
<div>
<?php echo $this->__(‘There are no products matching the selection. Please provide a category ID.’) ?>
</div>
<?php else: ?>
 
Then you can call this template by adding a block call like this in your CMS content pages:
 
 
{{block type="catalog/product_list" template="catalog/product/new_products.phtml"}}

No comments:

Post a Comment