Question

I want to fetch product collection sorted in desc order of created date and filter it out if the special price end date is lower than current date. I am able to sort it using this code.

if ($this->getCurrentOrder()) {
    if(($this->getCurrentOrder())=='position'){
        $this->_collection->setOrder('entity_id','desc');
    } else {
       $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
    }
}

But I cant filter this out further with special price end date < today's date. Can anyone help? I will really appreciate it.

Thanks.

Was it helpful?

Solution

$now = Mage::getSingleton('core/date')->gmtDate();
$this->_collection->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $now),
    1 => array('is' => new Zend_Db_Expr('null')))
 ), 'left')

The code above should allow only products that don't have an end date for the special price or if the end date is in the future.

[EDIT]
To add a filter for special_from_date you can add this after the code above

$this->_collection->addAttributeToFilter('special_from_date', array('or'=> array(
    0 => array('date' => true, 'to' => $now),
    1 => array('is' => new Zend_Db_Expr('null')))
), 'left') 
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top