KaMZaTa KaMZaTa - 23 days ago 6
PHP Question

How to check if a product is new in Magento?

How to check if a product is new in Magento 1.9.0.1?

I tried:

<?php if($_product->getNewProduct()) { ?>
...my HTML code...
<?php } ?>


but it dosn't work. What's wrong with it?

Answer

I solved in this way:

    <?php
        $current_date = Mage::getModel('core/date')->date('Y-m-d');
        $from_date = substr($_product->getNewsFromDate(), 0, 10);
        $to_date = substr($_product->getNewsToDate(), 0, 10); 

        $new = ($current_date >= $from_date && $current_date <= $to_date) || ($from_date == '' && $current_date <= $to_date && $to_date != '') || ($from_date != '' && $current_date >= $from_date && $to_date == '') ;
    ?>

    <?php if($new == 1) { ?>
             <img src="...
    <?php } ?>