【问题标题】:How to check if a product is new in Magento?如何在 Magento 中检查产品是否是新产品?
【发布时间】:2014-11-14 17:11:46
【问题描述】:

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

我试过了:

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

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

【问题讨论】:

  • 你在哪里试过这个?

标签: php magento magento-1.9.1


【解决方案1】:

不幸的是,您不能这么简单地做到这一点。没有办法检查一个产品是否是新的。

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

定义的主要方法是新产品收集方法。如果您查看该方法,即_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
    }
?>

欲了解更多信息,您可以使用此thread

【讨论】:

    【解决方案2】:

    我是这样解决的:

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

    【讨论】:

      【解决方案3】:

      我只想说news_from_date 和news_to_date 的产品is new depends on attribute

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

      【讨论】:

        【解决方案4】:

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

        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/

        【讨论】:

          【解决方案5】:
          <?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 $new 为真
          2. 从未设置日期和当前时间 $new 为真
          3. from_date $new 为 true

          【讨论】:

          • 三种情况:
          • 1. from_date $new 为真,2。从日期未设置且当前时间 $new 为真,3. from_date $new 为真
          • 哦,先解决了同样的问题,我只是复制到这个,我现在将其删除。 :)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-27
          • 2013-01-28
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          相关资源
          最近更新 更多