1

I have to get some data from order collection and I wrote this piece of code:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('customer_id', $customerId)
    ->addAttributeToFilter('increment_id', $incrementId)
    ->addAttributeToSelect('*');

The problem is that I want to make a field "increment_id" a optional one. If variable $incrementId will be a null the code:

-addAttributeToFilter('increment_id', $incrementId)

should be not considered.

2
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('customer_id', $customerId)
    ->addAttributeToSelect('*');
if (!is_null($incrementId)) {
    $order->addAttributeToFilter('increment_id', $incrementId);
}
| improve this answer | |
  • the simplest solutions are the best :) – Wojciech Ka Apr 5 '16 at 12:17

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.