Magento的addAttributeToFilter只获得特价产品

时间:2014-08-26 17:44:40

标签: php magento

我的客户想要一个只有特价的所有产品的页面。我试着把这段代码放在list.phtml页面上,但是没有显示任何内容,你能帮助我吗?

    $todayDate = date('m/d/y');
    $tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
    $tomorrowDate = date('m/d/y', $tomorrow);

    $_productCollection = $this->getLoadedProductCollection();

    $_productCollection = $_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or' => array(
        0 => array('date' => true, 'from' => $tomorrowDate),
        1 => array('is' => new Zend_Db_Expr('null')))
    ), 'left');

2 个答案:

答案 0 :(得分:-1)

**Setup a Magento Special Price Products Page** 

**First step:**

Create a new page named “Specials” (or whatever you want) in Admin->CMS->Pages and insert into “Custom Layout XML” field this code:

<reference name="content">
<block after="-" type="catalog/product_list" name="offerte.new" alias="offerte" template="catalog/product/specials.phtml">
<action method="setProductsCount"><count>15</count></action>
</block>
</reference>
select your layout (2columns-left,2columns-right or 3 columns ) and save.

**Second Step:**

Create this file:

app/design/frontend/default/YOURTHEME/template/catalog/product/specials.phtml

Populate the file with this content:

Mage::getSingleton('core/session', array('name' => 'frontend'));
$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);

$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

$_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
and with the content taken from

app/design/frontend/default/YOURTHEME/template/catalog/product/list.phtml

WITHOUT THIS LINE:

$_productCollection=$this->getLoadedProductCollection();
Save your template file and enjoy your specials page…

答案 1 :(得分:-1)

$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

$_productCollection = $this->getLoadedProductCollection();

//I put the clear() thing there and BUM!, and load() after to ;)

$_productCollection->clear()->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))->addAttributeToFilter('special_to_date', array('or' => array(
    0 => array('date' => true, 'from' => $tomorrowDate),
    1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->load();