When we work in magento with out of box customization then we might need to add feature for search order by phone in admin grid or somewhere else.

In this blog we are going to search order by phone number/telephone/mobile number in magento2.

As technially, we need to search phone in order’s address table and need to get order ids and use that order ids to get order information.

Here is code for search order by phone

PHP
<?php
declare(strict_types=1); //We recommend to use always strict_types = 1 for quality code

namespace MageDad\Module\Model;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory as AddressCollectionFactory;

class OrderSearch
{
  public function __construct(
    private OrderCollectionFactory $orderCollectionFactory,
    private AddressCollectionFactory $addressCollectionFactory
  ) { }

  /**
   * {@inheritdoc}
   */
  public function getOrderByPhone($phone)
  {
    $addressCollection = $this->addressCollectionFactory->create();
    $addressCollection->addFieldToFilter('telephone', ['eq' => $phone]);
    $orderIds = $addressCollection->getColumnValues('parent_id');
    
    if (count($orderIds) > 0) {
        $collection = $this->orderCollectionFactory->create();
        $collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
        return $collection->getItems();
    }
    
    return [];
  }
}

If you want to create API for search order by phone then you can refere GitHub module.

I hope this blog is useful to search order by phone in magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😂

87 Comments

  1. Howdy! Would you mind if I share your blog with my facebook group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Thanks

  2. It’s appropriate time to make some plans for the future and it’s time to be happy. I have read this post and if I could I desire to suggest you some interesting things or tips. Maybe you could write next articles referring to this article. I wish to read even more things about it!

  3. you are in reality a excellent webmaster. The web site loading pace is amazing. It sort of feels that you’re doing any distinctive trick. Also, The contents are masterwork. you have done a fantastic job in this matter!

  4. Hi, Neat post. There is a problem together with your site in web
    explorer, may check this? IE still is gambling casinos on the east coast (Vanessa) marketplace leader and a good element of
    other folks will omit your wonderful writing
    because of this problem.

  5. I used to be suggested this website via my cousin. I’m now not sure whether or not this post is written by way of him as nobody else recognize such designated approximately my trouble. You are incredible! Thank you!

  6. Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your theme. Bless you

  7. You made some first rate points there. I seemed on the internet for the issue and found most individuals will go along with together with your website.

  8. Thanks for the ideas you are giving on this blog. Another thing I’d prefer to say is getting hold of copies of your credit file in order to scrutinize accuracy of any detail is one first step you have to perform in repairing credit. You are looking to clear your credit file from destructive details faults that screw up your credit score.

Write A Comment