0

How can I get (if possible) information about whether the order is a guest checkout and if it is a logged in customer, it is a returning customer?

edit:

Unfortunately the previously marked solution does not work in all cases. Sometimes even though a customer has already ordered the order collection return nothing. I tried this alternative approach but it does not work as well:

<?php
// ...

private function getCustomerType(Order $order, Address $billingAddress = null, Address $shippingAddress = null)
{
    if ($order->getCustomerIsGuest()) {
        return self::ORDER_TYPE_GUEST;
    }

    $orderCollection = $this->orderCollectionFactory->create()->addAttributeToSelect('entity_id')
        ->addFieldToFilter('customer_email', $this->getUserEmail($order, $billingAddress, $shippingAddress));
    /** @var Order\Item $firstItem */
    $firstItem = $orderCollection->getFirstItem();
    if($firstItem instanceof Order\Item) {
        if (!empty($firstItem->getId())) {
            return self::ORDER_TYPE_RETURN;
        }
    }
    return self::ORDER_TYPE_NEW;
}
  • returning customer means ? – Keyur Shah Jan 15 '18 at 10:49
  • he ordered already – steros Jan 15 '18 at 10:51
1

You can use the following code to determine the order type:

<?php

class MyClass {

    const ORDER_TYPE_GUEST  = "guest checkout";
    const ORDER_TYPE_NEW    = "new customer";
    const ORDER_TYPE_RETURN = "returning customer";

    protected $orderCollectionFactory;

    public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    ){
        $this->_orderCollectionFactory = $orderCollectionFactory;
    }

    private function getCustomerType(Order $order)
    {
        if ($order->getCustomerIsGuest()) {
            return self::ORDER_TYPE_GUEST;
        }

        $orderCollection = $this->orderCollectionFactory
                           ->create()
                           ->addAttributeToSelect('entity_id')
                           ->addFieldToFilter('customer_email', $order->getCustomerEmail());
        if ($orderCollection->count()) {
            return self::ORDER_TYPE_RETURN;
        } else {
            return self::ORDER_TYPE_NEW;
        }
    }
}
  • Turns out this works in most cases but not all. Sometimes even though a customer has ordered already the orderCollection returns null :/ – steros Feb 16 '18 at 8:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.