E commerce
- March 25, 2009
Magento product : display new product icon on product list
If you’re working on Magento Ecommerce layouts and template design, you might have had a look at some nasty bugs, such the sort by price bug that’s occuring on Magento’s latest release (1.2..1). If you’ve spent a few minutes reading the fix for that bug on Magento’s forums, you know what product collection means. Product Collection in Magento means an easy way of listing products for specific blocks : the big drawback is that product collections store arrays of products, and products inside don’t always come fully loaded of details. That’s why the few hacks below are useful to get the very basic attribute of every product on a decent ecommerce store : is that product new or not, and should I tell my customer which products are new ???
How to load new attribute : in Magento the new attribute is defined by two database fields (or attributes) : new from and new to .
- Using these parameters in list.phtml requires you to modify the attribute set returned by the Mage_Catalog_Block_Product_List class to be found in app\code\core\Mage\Catalog\Block\Product\List.php
- in function _getProductCollection, paste the following line
$this->_productCollection
->addAttributeToSelect(’news_from_date’)
->addAttributeToSelect(’news_to_date’); - obvisouly this comes just after the product collection definition around line 75 of the List.php
$this->_productCollection = $layer->getProductCollection(); - your list.phtml template now has access to the data and checking that a product is new is as simple as performing PHP based date comparisons. We provide here basic comparison that will only work for products where both news_from and news_to have been filled. Add the following PHP code somewhere in your productcollection loop and the work is done !
$now = date(”Y-m-d”);
$newsFrom= substr($_product->getData(’news_from_date’),0,10);
$newsTo= substr($_product->getData(’news_to_date’),0,10);
if ($now>=$newsFrom && $now<=$newsTo)
{?> <h3 class=”nouveauteProductList” >NEW PRODUCT!<h3><?};
?>
This hack will be used on RueDeSiam’s website (to be released next month,bookmark it now!)




7 Responses to “Magento product : display new product icon on product list”
Which versions was this tested with? I cant get it to work wih 1.3.2! Where do I have to enter the code in the template file list.phtml? No matter where I enter it, the page will not be displayed or I will be getting an error message.
By nikl on Aug 21, 2009
Had my doubts at first reading previous comment. I had to try it for myself, and it indeed does work.
Two things:
1) If you’re copy/pasting the code, make sure to check the quote and double quote characters, may not be correct encoding.
2) In your list.phtml, make sure your putting the logic in the correct listing type. List is at the top, Grid is at the bottom. Obviously you won’t see any changes if you put the code in the List style type section and your store is displaying product as a Grid type.
Thanks for the code!
By sibble on Oct 1, 2009
where does i put this ?
i didn’t find ?
$now = date(”Y-m-d”);
$newsFrom= substr($_product->getData(’news_from_date’),0,10);
$newsTo= substr($_product->getData(’news_to_date’),0,10);
if ($now>=$newsFrom && $now NEW PRODUCT!
By nathan on Oct 8, 2009
You should not modify core Magento files. There is a way to do that whithout touching it.
In your /frontend/[you_template]/default/template/catalog/product/list.phtml add the following code to the top of the file :
–
$this->addAttribute(’news_from_date’);
$this->addAttribute(’news_to_date’);
function isProductNew($product) {
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$newsFrom = $product->getData(’news_from_date’);
if($todayDate >= $product->getData(’news_from_date’) && $todayDate getData(’news_to_date’))
{
return TRUE;
}
else
{
return FALSE;
}
}
–
Now you can test if the product is new by calling the function like :
–
echo (isProductNew($_product)) ? ‘New !’ : ”;
–
By mfauveau on Oct 22, 2009
Turns out Wordpress screwed up the code… let me try again :
function isProductNew($product) {
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
if($todayDate >= $product->getData(’news_from_date’) && $todayDate <= $product->getData(’news_to_date’))
{
return TRUE;
}
else
{
return FALSE;
}
}
By mfauveau on Oct 22, 2009
I still cannot get this to work. Im a little bit of a novice when it comes to coding, can you explain where to put the coding? and where to put this part:
echo (isProductNew($_product)) ? ‘New !’ : ”;
sorry, im so confused!
By jen on Nov 19, 2009
Hi,
In list.phtml file top of file put this
_productCollection
->addAttributeToSelect(’news_from_date’)
->addAttributeToSelect(’news_to_date’);
?>
and in place of example
after
htmlEscape($_product->getName()) ?>
place you code
getData(’news_from_date’),0,10);
$newsTo= substr($_product->getData(’news_to_date’),0,10);
if ($now>=$newsFrom && $now NEW PRODUCT!
By Naujasdizainas on Mar 2, 2010