1 post tagged

date

Check Product Is New

Magento has two attributes “Set Product As New From Date” and “Set Product As New To Date” to set a product as new. If both fields are empty the product is not new. If only “Set Product As New To Date” is set, the product will be marked as “new” from current time until “Set Product As New To Date”. And If you set only “Set Product As New From Date” attribute the product will be marked as new from that date until forever. But how to check is product “new” currently?

Unfortunately Magento doesn’t have special method for this. But in Mage_Core_Model_Locale model there is a method called isStoreDateInInterval($store, $dateFrom, $dateTo) which checks is current date in two passed dates interval. But we need additional validation two dates are not empty, otherwise that method will return true. So we can create such method in some helper class:

<?php
public 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);
}
2015   date   locale   new   product