2014-09-20 133 views
2

如何檢查產品是否是Magento 1.9.0.1中的新產品?如何檢查產品是否是Magento中的新產品?

我想:

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

,但它不到風度的工作。它出什麼問題了?

+0

你在哪裏試試這個? – 2014-09-20 06:49:07

回答

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

太好了。加工。 – 2016-03-04 05:23:48

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

1

在簡單的我想說的是,一個產品is new depends on attribute通過news_from_dat e和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 
    } 
2

您絕對不能手動轉換和驗證日期,因爲這會導致時區問題。 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://m-grater.info/all/check-product-is-new/

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

上面的代碼有三種情況:

  1. FROM_DATE < =當前時間< = to_date爲真 - > $ new爲真
  2. 未設定日期和當前時間< = TO_DATE是真實的 - > $新是真的
  3. FROM_DATE < =當前時間和TO_DATE沒有設置爲true - > $新是真的
+0

有三種情況: – 2016-11-26 08:30:41

+0

1. from_date <=當前時間<= to_date爲true => $ new爲true,2。 from date not set and current time <= to_date is true => $ new is true,3. from_date <=當前時間並且to_date未設置爲true => $ new爲真 – 2016-11-26 08:34:30

+0

哦同樣先解決,我只是複製到這個,我現在要刪除它。 :) – 2016-11-26 08:49:04

相關問題