2

如何检查 Magento 1.9.0.1 中的产品是否是新产品?

我试过了:

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

但它不起作用。它出什么问题了?

4

5 回答 5

3

您绝对不能手动转换和验证日期,因为这可能会导致时区问题。isStoreDateInInterval()Magento在Mage_Core_Model_Locale类中有内置方法。所以你应该创建看起来像这样的辅助方法

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

您可以在此处找到更多详细信息http://quicktips.ru/all/check-product-is-new/

于 2015-09-20T18:41:29.963 回答
2

简单地说,我想说的is new depends on attributenews_from_date 的产品和news_to_date

$todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);


 $collection = Mage::getResourceModel('catalog/product_collection');
  $collection 
            ->addAttributeToFilter('news_from_date', array('or'=> array(
                0 => array('date' => true, 'to' => $todayEndOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayStartOfDayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter(
                array(
                    array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                    )
              )
               ->addAttributeToFilter('entity_id',$_product->getId())
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize(1)
            ->setCurPage(1);    
    if($count($collection)>0){
        //new products
    }
于 2014-09-20T19:34:44.483 回答
2
<?php 

$current_date=M age::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 == '') ; 
        //echo $new; ?>

<?php if($new==1 ) { ?>
<label for="">
    <?php echo $this->__('New') ?></label>
<?php } ?>

上面的代码有3种情况:

  1. from_date <= 当前时间 <= to_date 为真 --> $new 为真
  2. 从未设置日期和当前时间 <= to_date 为 true --> $new 为 true
  3. from_date <= 当前时间和 to_date 未设置为 true --> $new 为 true
于 2016-11-16T03:43:56.567 回答
1

我是这样解决的:

    <?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 } ?>
于 2014-09-20T18:39:57.077 回答
1

不幸的是,您不能这么简单地做到这一点。不存在检查产品是否新的方法。

为新产品定义的块类Mage_Catalog_Block_Product_Newapp\code\core\Mage\Catalog\Block\Product\New.php. 如果你在那里看看,那么你就会明白,没有对你有用的好方法。

定义的主要方法是新产品收集方法。如果您查看该方法,即 on _getProductCollection(),您可以看到它使用开始日期和结束日期(即我们为每个产品设置)来制作集合。因此,您可以实现的最佳方法是检查产品的开始日期和结束日期,它是否在我们想要的日期内,然后在您的if声明中执行代码。它会看起来像这样。

<?php 
    if (date("Y-m-d") >= substr($_product->getNewsFromDate(), 0, 10)         
        && date("Y-m-d") <= substr($_product->getNewsToDate(), 0, 10)) {
        //your code comes here
    }
?>

有关更多信息,您可以使用此线程

于 2014-09-20T07:06:51.913 回答