0

When I try to add a field to filter on my magento 2.2.4 quote collection, the quote are not arranged in the right order.

$customerId  = $this->_customerSession->getCustomer()->getId();
            echo $customerId; // 2
            $collection = $this->collection->create();
            $collection->addFieldToFilter('customer_id', customerId);

This code reverses my last two ids (121 is after 120).

But this :

        $customerId  = $this->_customerSession->getCustomer()->getId();
        echo $customerId;
        $collection = $this->collection->create();
        echo "<pre>".print_r($collection->getData(), true)."</pre>";

Is giving me the right order. But i need to filter $collection by customer_id. How do I do that ?

1 Answer 1

3

You can add a sort order to the collection (before it's loaded):

$field = 'customer_id';
$collection->addOrder(
    $field,
    'DESC' // 'ASC'
);
1
  • 1
    Yes, I found the solution exactly at the same time you said yours :) Jul 25, 2018 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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