I am implementing a feature, where I need to filter the order item list in order admin page. I have checked and found the list is getting from method getItemsCollection(). But when I am trying to filter the items collection, its not working.

Then for further debugging, I have tried to add filter in getItemsCollection() method of Magento\Sales\Model\Order class.

public function getItemsCollection($filterByTypes = [], $nonChildrenOnly = false)
    $collection = $this->_orderItemCollectionFactory->create()->setOrderFilter($this);

    if ($filterByTypes) {
    if ($nonChildrenOnly) {

    if ($this->getId()) {
        foreach ($collection as $item) {
    return $collection;

Here when I added filter using below code, it was working if it added before foreach but if I add below code just before return, its not working.

$collection->addFieldToFilter('custom_variable', 'value');

If anyone faced this issue earlier or know why this is happening please let me know.

  • print query log and check if your custom_variable is there or not – Rakesh Varma Mar 25 at 7:38
  • It is there and I have printed the query. Query is prepared correctly and getSize() is giving me correct number. But items are not getting filtered. – Gaurav Agrawal Mar 25 at 9:21
  • If you can print the query, you can run it directly on your database, then you can read/analyze the SQL to see where it doesn't match your requirement. – Jimmy Mar 26 at 3:27

Your Answer

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

Browse other questions tagged or ask your own question.