16

From a quick search the only reference I found was on the Magento forums which is suggesting that you have to write some custom code to check if a product is new.

I would have thought there would be a simple isNew() method on the Mage_Catalog_Model_Product which takes into account the Featured dropdown as well as the Set Product As New From Date and Set Product As New To Date fields - but that doesn't seem to be the case?

Are teh custom codez needed?

3
  • isNew() is only checking if this is new to the DB (ie it hasn't been saved yet and it does not yet have an entity id, most of the time the autoincrement) Commented Mar 7, 2014 at 21:38
  • @Fooman I think that's isObjectNew() but I get what you're saying. Commented Mar 7, 2014 at 22:01
  • Check this link. It states how to do it. universalcoder.wordpress.com/2014/04/14/…
    – Dexter
    Commented Apr 14, 2014 at 12:28

6 Answers 6

14

You absolutely must NOT convert and validate dates manually, because this can cause timezone issues. Magento has built-in method isStoreDateInInterval() in Mage_Core_Model_Locale class. So you should create helper method which can look like this

function isProductNew(Mage_Catalog_Model_Product $product)
{
    $newsFromDate = $product->getNewsFromDate();
    $newsToDate   = $product->getNewsToDate();
    if (!$newsFromDate && !$newsToDate) {
        return false;
    }
    return Mage::app()->getLocale()
            ->isStoreDateInInterval($product->getStoreId(), $newsFromDate, $newsToDate);
}

More details you can find here http://quicktips.ru/all/check-product-is-new/

1
  • Shoot good call - thanks for that. I've updated my answer real quick. Commented Sep 24, 2015 at 20:57
6

There's no single method call. At least I'm not aware of it.

Nevertheless you can always use something like this:

<?php if (date("Y-m-d") >= substr($_product->getNewsFromDate(), 0, 10) && date("Y-m-d") <= substr($_product->getNewsToDate(), 0, 10)) : ?>
...
<?php endif ?>

Sorry I'm not good in PHP date comparison.

Good question by the way.

1
  • I adapted your function to show the product who don't have getNewsToDate definied too. <?php if ((date("Y-m-d") >= substr($_product->getNewsFromDate(), 0, 10) && $_product->getNewsFromDate()!="") && (date("Y-m-d") <= substr($_product->getNewsToDate(), 0, 10) || $_product->getNewsToDate()=="")) : ?> ... <?php endif ?>
    – Eduardo
    Commented Sep 12, 2016 at 20:09
5

For reference, here's the helper method that I came up with to check for newness. It supports both the Featured dropdown as well as the dates, and supports either date being empty as well.

public function isNew($product)
{
    if ($product->getData('featured_product')) {
        return true;
    }

    if ($product->getData('news_from_date') == null && $product->getData('news_to_date') == null) {
        return false;
    }

    if ($product->getData('news_from_date') !== null) {
        if (date('Y-m-d', strtotime($product->getData('news_from_date'))) > date('Y-m-d', time())) {
            return false;
        }
    }

    if ($product->getData('news_to_date') !== null) {
        if (date('Y-m-d', strtotime($product->getData('news_to_date'))) < date('Y-m-d', time())) {
            return false;
        }
    }

    return true;
}

UPDATE: Thanks to @Rooooomine for mentioning that this manual date conversion I suggested is a very bad idea due to potential locale issues. Check out Mage::app()->getLocale()->isStoreDateInInterval($product->getStoreId(), $newsFromDate, $newsToDate) instead.

3

A precision that might be useful to someone. All these answers are using the attribute "news_from_date" but if you want to get the actual date of creation of the product in the database, you should get the "created_at" attribute using the method:

$product->getCreatedAt()

In the project I'm working on, some products might have a value of "news_from_date" different from "created_at". Indeed according to the business rules, a product can be out of stock for a long time and then come back as new products when it comes back in the catalog.

0

Quick Solution

    <?php
    foreach ($_productCollection as $_product):
    $date = date("Y-m-d 00:00:00");
        $newtodate = $_product['news_to_date'];
        $newfromdate = $_product['news_from_date'];

    if (isset($newfromdate) and isset($newtodate) and $date >= $newfromdate and $date <= $newtodate) 
       {
          echo "Product is new";
        }?>
     <?php endforeach ?>
1
  • 2
    Thanks Chirag. Only thing bothering me a bit about this code example is the indentation! :) Commented Mar 10, 2014 at 15:23
0

You can try below code

 $current_date = new DateTime($date); // compare date
 $from_date = new DateTime($this->getData('news_from_date')); // begin date
 $to_date = new DateTime($this->getData('news_to_date')); // end date        
 $new = ($current_date >= $from_date && $current_date <= $to_date);

Where $this is object of Mage_Catalog_Model_Product and you need to load the product if it is not already loaded.

1
  • 1
    Thanks Anshu - I think this might not support empty from or to dates though. Commented Mar 10, 2014 at 15:27

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.