Magento 2 – Get List of Guest Orders

collection;magento-2.1magento2orders

I want to show list of the guest user in my custom module. Which is the best way to get the list of the guest order.

How can i get list of the guest orders?

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $lastyear = date('Y-m-d', strtotime("-1 year"));

  $order = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('customer_id', $customer_id)->addAttributeToFilter('state', 'complete')->addAttributeToFilter('created_at', ['gteq'  => $lastyear]);

To get the guest orders i am getting collection of all the orders and checking customer email address. If user with that email address doesn't exist then consider it as guest order.

Is this correct flow? Is there any other flow to check guest orders?

Any help would be appreciated.

Best Answer

You can get guest customer order list by below code.

public function __construct(
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,

) {
    $this->orderCollectionFactory = $orderCollectionFactory;
}

public function getGuestOrderCollection()
{
    $orderCollecion = $this->orderCollectionFactory
        ->create()
        ->addFieldToSelect('*');

    $orderCollecion->addAttributeToFilter('customer_is_guest', ['eq'=>1]);

    echo "<pre>";
    print_r($orderCollecion->getData());
    exit; 
    return $orderCollecion;
}
Related Topic