2016-03-11 63 views
0

我需要限制產品相對於日期的列表。在magento中使用日期限制產品

比如我需要顯示2015年1月1日之間產生的產品,以2016年1月1日

這裏是我的代碼

$products=Mage::getModel('catalog/product')->getCollection(); 
    //$products->addAttributeToSelect('name'); 
    $products->addAttributeToSelect(array('name', 'thumbnail', 'price','description','special_price')); 

foreach ($products as $product) { 
$p['products'][] = array(
      'id'  => $product->getId(), 
      'sku'  => $product->getSku(), 
      'name'  => $product->getName(), 
      'thumb'  => (string)Mage::helper('catalog/image')->init($product, 'thumbnail'), 
     'description' => $product->getDescription(), 
      'weight'  => $product->getWeight(), 
     'created at' => $product->getCreatedAt(), 
      'pirce'  => Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode, 
     'Special pirce' => Mage::helper('core')->currency($product->getFinalPrice(), true, false) 



     ); 
} 

var_dump($result); 

請建議我一個解決方案

回答

2
$products=Mage::getModel('catalog/product')->getCollection(); 
    //$products->addAttributeToSelect('name'); 
    $products->addAttributeToSelect(array('name', 'thumbnail','price','description','special_price')); 

$first = date('m/j/Y', strtotime('2015-01-01'));  
$last = date('m/j/Y', strtotime('2016-01-01')); 

//--please add these filters 
    $products->addAttributeToFilter('created at', array('gteq' =>$first)); 
    $products->addAttributeToFilter('created at', array('lteq' => $last)); 

foreach ($products as $product) { 
$p['products'][] = array(
      'id'  => $product->getId(), 
      'sku'  => $product->getSku(), 
      'name'  => $product->getName(), 
      'thumb'  => (string)Mage::helper('catalog/image')->init(
. 
. 
. 
. 
+0

致命錯誤:調用一個成員函數getBackend()一個非對象在/app/code/core/Mage/Eav/Model/Entity/Abstract.php線816 –

+0

其工作,謝謝 –

+0

爲什麼會出現這種情況: - 調用成員函數getBackend()在/app/code/core/Mage/Eav/Model/Entity/Abstract.php中的非對象 –