0

I want to display orders that are in status pending 4 days after the creation date.

In sql this work with this => SELECT * FROM sales_flat_order where DATE_ADD(updated_at, INTERVAL 4 DAY) and status = pending;

But with orm in magento this not work why ?

  $collection = Mage::getModel('sales/order')->getCollection()
                ->addFieldToFilter('status', 'pending')
                ->addFieldToFilter('updated_at',  new Zend_Db_Expr("DATE_ADD('updated_at', INTERVAL 4 DAY)")); 

that does not give me the result I expect from my sql query at the top.

Can you help me please

1

The issue is that you can't use addFieldToFilter to add a DB Expression. You will need to modify the base query by fetching the select.

/** @var Mage_Sales_Model_Entity_Order_Collection $collection */
$collection = Mage::getModel('sales/order')->getCollection();
$collection->addFieldToFilter('status', 'pending');
$collection->getSelect()->where(new Zend_Db_Expr("DATE_ADD('updated_at', INTERVAL 4 DAY)"));

var_dump((string) $collection->getSelect());

You can also print the query by casting to string the getSelect which make it easier to debug.

1
  • Thank you Oliver de Cramer, but i need to return collection for send email. When i debug $collection, i don't have information of order.
    – devmagento
    Nov 21 '17 at 11:15
0

=> This is your code

public function getCollectionStandByPayement(){


$collection = Mage::getModel('sales/order')->getCollection();
$collection->addFieldToFilter('status', 'pending');
$collection->getSelect()->where(new Zend_Db_Expr("DATE_ADD('updated_at', INTERVAL 4 DAY)"));

    return $collection;


}

and

 public function getOrderStandByPayement() {

    foreach ($this->getCollectionStandByPayement() as $order) {

        Mage::log('N° = ' .$order->getIncrementId() .' - '. $order->getCreatedAt(), null, 'getOrderStandByPayement.log', true);
        $this->sendEmailStandByPayement($order);    
    }
}

when I debug $collection I don't have information of order

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.