1

I have placed an order from guest account and the end of order I have given an option to create an Account, when I created the account and navigate to order sections i see no others. So i expplored a bit but still i am not able to understand.

Right now my code loads the order information via this code

   public function getOrderInformation($orderid)
   {
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);
     return $order;
   }


    $shippingAddressObj = $orderInfo->getShippingAddress();
    $shippingAddressArray = $shippingAddressObj->getData();
    $paymentObj = $orderInfo->getPayment();
    $paymentArray = $paymentObj->getData();

also for order related information like name, quantity, size I use this

    <?php foreach ($orderInfo->getAllVisibleItems() as $item): ?>
    <?php endforeach; ?>

How can I achieve the same for guest user order, as I will be using the same phtml for my account(guest or registered user).

1 Answer 1

0

You have used a data model class Magento\Sales\Api\Data\OrderInterface for you does not getting the data. You have to use Order repository interface Magento\Sales\Api\OrderRepositoryInterfaceor order repository Magento\Sales\Model\OrderRepository for getting an order data.

If you have order Id then using Magento\Sales\Model\OrderRepository:get($id) method, you will get orde details which you want event if place as register customer or guest customer.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $order = $objectManager->create('Magento\Sales\Api\OrderRepositoryInterface')->get($orderid);

$shippingAddressObj = $order->getShippingAddress();
$shippingAddressArray = $order->getData();
$paymentObj = $order->getPayment();
$paymentArray = $order->getData();

Get Guest Customer Orders

<?php


namespace {namespace};

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SortOrderBuilder;

class {Class}     {

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var SortOrderBuilder
     */
    private $sortOrderBuilder;

    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder  $searchCriteriaBuilder,   
        FilterBuilder $filterBuilder,
        SortOrderBuilder $sortOrderBuilder      
    ) {


        $this->filterBuilder = $filterBuilder;
        $this->sortOrderBuilder = $sortOrderBuilder;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository = $orderRepository;
    }

    public function getGuestOrders()
    {
        $customerEmail  = '[email protected]';
        $filter1 = $this->filterBuilder->create()->setField('customer_email')
                ->setConditionType('eq')->setValue($customerEmail);
        $filter2 = $this->filterBuilder->create()->setField('customer_is_guest')
                ->setConditionType('eq')->setValue(1);
        $searchCriteria =$this->searchCriteriaBuilder->addFilters([$filter1,$filter2])
                ->create();
       $result = $this->orderRepository->getList($searchCriteria);
       if($result->getTotalCount() >0)
       {
           $orders = $result->getItems();
           foreach($orders as $order)
           {
                $guestCustomer = $order->getCustomerIsGuest();
                $groupId  = $order->getCustomerGroupId();
                $firstname = $order->getCustomerFirstname();
                $lastname = $order->getCustomerMiddlename();
                $lastname = $order->getCustomerLastname();
                $prefix = $order->getCustomerPrefix();
                $suffix = $order->getCustomerSuffix();
                $dob = $order->getCustomerDob();
                $taxvat = $order->getCustomerTaxvat();
                $gender = $order->getCustomerGender();
                $orderItems = $order->getItems();


           }
       }
    }
}
2
  • How can i get order id for guest user order
    – summu
    Feb 14, 2019 at 10:44
  • So I have to write a different function to get orders for guest checkout orders? Even though after placing an order he creates his/her account. Then to display all orders in my account page, i used your code OrderRepositoryInterface. This will give me orders of guest too after placing order ?
    – summu
    Feb 14, 2019 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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