2

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.

4
$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') 
  • Awesome. Works great. Thank you so much.. – hks Jun 12 '15 at 7:37
  • How can I filter if it should only show if today's date is between special price from date and special price to date? Kind of special price from date should be lower than today's date and special price to date should be higher than today's date. special price from date < today's date & special price to date > today's date. Thanks. – hks Jun 17 '15 at 7:42
  • @hks. see my edit. – Marius Jun 17 '15 at 7:48
  • Thank you so much. That works great. Do you have any reference link where I can learn to make this kind of queries in Magento? – hks Jun 17 '15 at 8:12
  • I took these and addapted them from the Catalog/Block/Product/New.php class. – Marius Jun 17 '15 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.