cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 get list of the guest orders

SOLVED

Magento 2 get list of the guest orders

I want to get the list of the orders placed by guest users.  I want to display the guest user separately. 

 

Is there any way to do so?  How can i get collection of guest order.  Right now i am fetching all the orders and retriving the customer email address to check it's registered or not.

If not registered then we mark it as guest order.  

Is this the correct and best approch to do that?  Is there any other way to do this?  How can i do this programmatically?

 

I tried below code to get the list of the 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]);

Is there any better way to do this? 

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 get list of the guest orders

Thanks for the reply.    I found solution myself.

 

created module and

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;
}

 

Link: Answer posted here:  https://magento.stackexchange.com/questions/211477/magento-2-get-list-of-the-guest-orders

View solution in original post

3 REPLIES 3

Re: Magento 2 get list of the guest orders

Try below code to get list of the guest orders.

 

 

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]);

print_r($orderCollecion->getData()); exit; return $orderCollecion; }

 

If you want to do that with ObjectManager Then try below code :

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')
            ->getCollection()
            ->addAttributeToFilter('customer_is_guest', ['eq'=>1]);

Note : ObjectManager way is not preferable as per magento 2 guideline.

 

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 get list of the guest orders

IF you need to only get Guest Customer Order,

You can do as below,

 

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

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

Need to filter using customer_is_guest to For guest customer field with value is 1 and for the registered customer its value is 0.

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Magento 2 get list of the guest orders

Thanks for the reply.    I found solution myself.

 

created module and

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;
}

 

Link: Answer posted here:  https://magento.stackexchange.com/questions/211477/magento-2-get-list-of-the-guest-orders