0
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

2
  • Whats the class for customerFactory
    – Black
    Dec 8 '20 at 10:37
  • \Magento\Customer\Model\CustomerFactory
    – user92743
    Dec 12 '20 at 7:04
0

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();
    }
}
4
  • Hi,thank you for answer,but is not working. Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::save()
    – user92743
    Dec 8 '20 at 9:59
  • I can print all of data,but don't save data in database
    – user92743
    Dec 8 '20 at 10:02
  • answer is updated. Dec 8 '20 at 10:34
  • It is not working for me, becuase it is no parent. parent::__construct($context, $data);@madani
    – user92743
    Dec 12 '20 at 7:01
0

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);
    }
}

Your Answer

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