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