使用magento中的日期限制产品

时间:2016-03-11 10:02:59

标签: magento magento-1.9

我需要限制与日期相关的产品列表。

例如,我需要显示在1.1.2015到1.1.2016之间创建的产品

这是我的代码

   $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 个答案:

答案 0 :(得分: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(
.
.
.
.

答案 1 :(得分:0)

请使用magento-collection的过滤器。 https://gyazo.com/7d4b90d626b09c4a4fafd69c9f540e9d