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 ???

magento-new-product490

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

9 replies
  1. Anthony Charrex
    Anthony Charrex says:

    To compare date correctly:

    getLocale()->isStoreDateInInterval(Mage::app()->getStore(), $_product->getData('news_from_date'), $_product->getData('news_to_date'))) {
    // is new
    }

    Reply
  2. Kamil W?grzynowicz
    Kamil W?grzynowicz says:

    You don't have to rewrite anything. And you really shouldn't put any code directly in Magento core. To add any attribute to the product collection just modify config.xml of any module or create XMl in app/etc/modules directory (name doesn't matter, because all XML files in this directory are merged by Magento) and put this code:

    And that's it! No rewrite needed, just add it via XML

    Reply
  3. Naujasdizainas
    Naujasdizainas says:

    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!

    Reply
  4. jen
    jen says:

    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!

    Reply
  5. mfauveau
    mfauveau says:

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

    Reply
  6. mfauveau
    mfauveau says:

    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 !' : '';

    --

    Reply
  7. nathan
    nathan says:

    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!

    Reply
  8. sibble
    sibble says:

    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!

    Reply
  9. nikl
    nikl says:

    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.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *