Question

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $email   = $observer->getEvent()->getData('email');
    $address = $observer->getEvent()->getData('address');

    // Get Website ID
    $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();

    // Instantiate object (this is the most important part)
    $password  = $this->encryptor->getHash($this->createRandomPassword(9), true);
    $customer  = $this->customerFactory->create();
    $customer->setWebsiteId($websiteId)
            ->setStoreId($this->storeManager->getStore()->getId())
            ->setEmail("abc@abc.com")
            ->setFirstname("Allen")
            ->setLastname("Allen")
            ->setGroupId(1);

    $newCustomer = $this->customerRepository->save($customer, $password);
    echo $newCustomer->getId();
}

I can print customer id,but database is void

Was it helpful?

Solution 2

I have solved by this code

/**
 * @var \Magento\Sales\Model\OrderFactory
 */
protected $_orderFactory;

/**
 * @var \Magento\Sales\Api\OrderCustomerManagementInterface
 */
protected $_orderCustomerService;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $_storeManager;

/**
 * @var \Magento\Customer\Model\CustomerFactory
 */
protected $_customer;

/**
 * @var \Magento\Sales\Api\OrderRepositoryInterface
 */
protected $_orderRepository;

/**
 * Convertguest constructor.
 * @param \Magento\Sales\Model\OrderFactory $orderFactory
 * @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
 * @param \Magento\Customer\Model\CustomerFactory $customer
 * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 */
public function __construct(
    \Magento\Sales\Model\OrderFactory $orderFactory,
    \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService,
    \Magento\Customer\Model\CustomerFactory $customer,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->_orderFactory = $orderFactory;
    $this->_orderCustomerService = $orderCustomerService;
    $this->_customer = $customer;
    $this->_orderRepository = $orderRepository;
    $this->_storeManager = $storeManager;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order_id  = $observer->getEvent()->getData('order_id');

    $order = $this->_orderFactory->create()->load($order_id);

    $customer= $this->_customer->create();
    $customer->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
    $customer->loadByEmail($order->getCustomerEmail());

    /*Convert guest to customer*/
    if ($order->getId() && !$customer->getId()) {
        /*New Customer*/
        $this->_orderCustomerService->create($order_id);
    } else {
        /*Registered customer guest checkout*/
        $order->setCustomerId($customer->getId());
        $order->setCustomerIsGuest(0);
        $this->_orderRepository->save($order);
    }
}

OTHER TIPS

try the below solution,

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    array $data = []
) {
    $this->storeManager = $storeManager;
    $this->customerFactory = $customerFactory;
    parent::__construct($context, $data);
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $email   = $observer->getEvent()->getData('email');
    $address = $observer->getEvent()->getData('address');
    
    $store = $this->storeManager->getStore();
    $storeId = $store->getStoreId();
    $websiteId = $this->storeManager->getStore()->getWebsiteId();
    $customer = $this->customerFactory->create();
    $customer->loadByEmail($email);
    if(!$customer->getId()){
        $customer->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname('Allen')
            ->setLastname('Allen')
            ->setEmail($email)
            ->setGroupId('1');
        $customer->save();
        echo $customer->getId();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top