Magento product collection fetch based on special price end date in desc order of created date
-
13-12-2019 - |
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.
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