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.
Solved! Go to Solution.
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
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 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 1 For guest customer field with value is 1 and for the registered customer its value is 0.
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