3

I have added customer_register_succes event for guest customer account creation during guest checkout. After the successful order when the user sign up by my event and observers call in which I load order for that guest user.

I have added event and observer for sign up and login of customer

<event name="customer_register_success">
    <observer instance="YX\Customer\Observer\RegisterSuccess" name="customer_register_succes"/>
</event>

<event name="customer_login">
    <observer name="customer_login_observer" instance="YX\Customer\Observer\CustomerLogin" />
</event>

Observer code

 class RegisterSuccess implements ObserverInterface
{ 

protected  $customlogger;
protected  $tempblock;
protected  $customerHelper;
protected $_blockFactory;


public function __construct(
    \YX\Catalog\Block\OrderHelper $orderHelper,
    \YX\Customer\Helper\CustomerHelper $customerHelper,
    \Magento\Framework\View\Element\BlockFactory $blockFactory

)
{
    $this->_blockFactory = $blockFactory;
    $this->tempblock = $this->_blockFactory->createBlock('YX\Catalog\Block\OrderHelper');
    $this->customerHelper = $customerHelper;

}
public function execute(\Magento\Framework\Event\Observer $observer) {

    if (!($this->customerHelper->isCustomer())){

        $customerID = $this->customerHelper->getCustomerId();
        $orderId = $this->customerHelper->getOrderId();
        $this->tempblock->guestToCustomer($orderId,$customerID);

    }
  }
}

getCustomerId function

 public function getCustomerId(){
    $customerId =  $this->customerSession->getCustomer()->getId();
    return $customerId;
}

getOrderId function

 public  function  getOrderId() {

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();

    $customerSession = $objectManager->create('Magento\Customer\Model\Session');

    if ($customerSession->isLoggedIn()) {
        $customer_email = $customerSession->getCustomer()->getEmail();
        $order_collection = $objectManager->create('Magento\Sales\Model\Order')->getCollection()
            ->addFieldToSelect('entity_id')
            ->addAttributeToFilter('customer_email', $customer_email)
            ->addAttributeToFilter('customer_is_guest', 1);

        foreach ($order_collection as $data){
            $order = $data->getEntityId();
            return $order;
        }
    }
}

guestToCustomer function

public function guestToCustomer($incrementId,$customerId)
{

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

 if ($order->getId() && !$order->getCustomerId()) {
     $order->setCustomerId($customerId);
     $order->setCustomerIsGuest(0);
     $this->orderRepository->save($order);
  }
 }

I am getting this error

An ID is needed. Set the ID and try again.

enter image description here

I am not able to figure out what's happening, and when I login with the just created account I can see my orders as I have events and observer for login also.

It works for login, not for sign up process. and after this, the first time user(new user) sign up process gives me same error.

Despite that error I even check the customer_entity table and it has customer entry.

select * from customer_entity order by created_at desc limit 1;

0

Replace your observer class by following class and modify namespace:


<?php
namespace SR\MagentoCommunity\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Model\OrderRepository;

class CustomerRegisterSuccess implements ObserverInterface
{
    /**
     * @var SearchCriteriaBuilderFactory
     */
    private $searchCriteriaBuilderFactory;

    /**
     * @var OrderRepository
     */
    private $orderRepository;

    /**
     * CustomerRegisterSuccess constructor.
     *
     * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
     * @param OrderRepository $orderRepository
     */
    public function __construct(
        SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
        OrderRepository $orderRepository
    ) {
        $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
        $this->orderRepository = $orderRepository;
    }

    /**
     * @param EventObserver $observer
     * @return $this
     */
    public function execute(EventObserver $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $customerId = $customer->getId();
        $orderCollection = $this->getOrderCollection($customer->getEmail());
        foreach ($orderCollection as $order) {
            if ($order->getEntityId() && !$order->getCustomerId()) {
                $order->setCustomerId($customerId);
                $order->setCustomerIsGuest(0);
                $this->orderRepository->save($order);
            }
        }
        return $this;
    }

    /**
     * @param string $customerEmail
     * @return \Magento\Sales\Api\Data\OrderInterface[]
     */
    private function getOrderCollection($customerEmail)
    {
        /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
        $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
        $searchCriteriaBuilder
            ->addFilter('customer_email', $customerEmail)
            ->addFilter('customer_is_guest', 1);

        return $this->orderRepository
            ->getList($searchCriteriaBuilder->create())
            ->getItems();
    }
}

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.