/** * Edit/View Existing Customer form fields * * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset * @return string[] Values to set on the form */ protected function _addEditCustomerFormFields($fieldset) { $fieldset->getForm()->getElement('created_in')->setDisabled('disabled'); $fieldset->getForm()->getElement('website_id')->setDisabled('disabled'); $customerData = $this->_getCustomerDataObject(); if ($customerData->getId() && $this->_accountManagement->isReadonly($customerData->getId())) { return []; } // Prepare customer confirmation control (only for existing customers) $confirmationStatus = $this->_accountManagement->getConfirmationStatus($customerData->getId()); $confirmationKey = $customerData->getConfirmation(); if ($confirmationStatus != AccountManagementInterface::ACCOUNT_CONFIRMED) { $confirmationAttr = $this->_customerMetadata->getAttributeMetadata('confirmation'); if (!$confirmationKey) { $confirmationKey = $this->_getRandomConfirmationKey(); } $element = $fieldset->addField('confirmation', 'select', ['name' => 'confirmation', 'label' => __($confirmationAttr->getFrontendLabel())]); $element->setEntityAttribute($confirmationAttr); $element->setValues(['' => 'Confirmed', $confirmationKey => 'Not confirmed']); // Prepare send welcome email checkbox if customer is not confirmed // no need to add it, if website ID is empty if ($customerData->getConfirmation() && $customerData->getWebsiteId()) { $fieldset->addField('sendemail', 'checkbox', ['name' => 'sendemail', 'label' => __('Send Welcome Email after Confirmation')]); return ['sendemail' => '1']; } } return []; }
/** * Check if account is confirmed * * @return \Magento\Framework\Phrase */ public function getIsConfirmedStatus() { $id = $this->getCustomerId(); switch ($this->accountManagement->getConfirmationStatus($id)) { case AccountManagementInterface::ACCOUNT_CONFIRMED: return __('Confirmed'); case AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED: return __('Confirmation Required'); case AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED: return __('Confirmation Not Required'); } return __('Indeterminate'); }
/** * Involve new customer to system * * @return $this */ protected function _involveNewCustomer() { $customer = $this->getQuote()->getCustomer(); $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId()); if ($confirmationStatus === \Magento\Customer\Model\AccountManagement::ACCOUNT_CONFIRMATION_REQUIRED) { $url = $this->_customerUrl->getEmailConfirmationUrl($customer->getEmail()); $this->messageManager->addSuccess(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $url)); } else { $this->getCustomerSession()->loginById($customer->getId()); } return $this; }
/** * Create customer account action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) { $resultRedirect->setPath('*/*/'); return $resultRedirect; } if (!$this->getRequest()->isPost()) { $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]); $resultRedirect->setUrl($this->_redirect->error($url)); return $resultRedirect; } $this->session->regenerateId(); try { $address = $this->extractAddress(); $addresses = $address === null ? [] : [$address]; $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); $customer->setAddresses($addresses); $password = $this->getRequest()->getParam('password'); $confirmation = $this->getRequest()->getParam('password_confirmation'); $redirectUrl = $this->session->getBeforeAuthUrl(); $this->checkPasswordConfirmation($password, $confirmation); $customer = $this->accountManagement->createAccount($customer, $password, $redirectUrl); if ($this->getRequest()->getParam('is_subscribed', false)) { $this->subscriberFactory->create()->subscribeCustomerById($customer->getId()); } $this->_eventManager->dispatch('customer_register_success', ['account_controller' => $this, 'customer' => $customer]); $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId()); if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { $email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail()); // @codingStandardsIgnoreStart $this->messageManager->addSuccess(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $email)); // @codingStandardsIgnoreEnd $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]); $resultRedirect->setUrl($this->_redirect->success($url)); } else { $this->session->setCustomerDataAsLoggedIn($customer); $this->messageManager->addSuccess($this->getSuccessMessage()); $resultRedirect = $this->accountRedirect->getRedirect(); } return $resultRedirect; } catch (StateException $e) { $url = $this->urlModel->getUrl('customer/account/forgotpassword'); // @codingStandardsIgnoreStart $message = __('There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.', $url); // @codingStandardsIgnoreEnd $this->messageManager->addError($message); } catch (InputException $e) { $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage())); foreach ($e->getErrors() as $error) { $this->messageManager->addError($this->escaper->escapeHtml($error->getMessage())); } } catch (\Exception $e) { $this->messageManager->addException($e, __('We can\'t save the customer.')); } $this->session->setCustomerFormData($this->getRequest()->getPostValue()); $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]); $resultRedirect->setUrl($this->_redirect->error($defaultUrl)); return $resultRedirect; }
/** * Saving customer subscription status * * @param int $customerId * @param bool $subscribe indicates whether the customer should be subscribed or unsubscribed * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _updateCustomerSubscription($customerId, $subscribe) { try { $customerData = $this->customerRepository->getById($customerId); } catch (NoSuchEntityException $e) { return $this; } $this->loadByCustomerId($customerId); if (!$subscribe && !$this->getId()) { return $this; } if (!$this->getId()) { $this->setSubscriberConfirmCode($this->randomSequence()); } $sendInformationEmail = false; $status = self::STATUS_SUBSCRIBED; if ($subscribe) { if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED == $this->customerAccountManagement->getConfirmationStatus($customerId)) { $status = self::STATUS_UNCONFIRMED; } } else { $status = self::STATUS_UNSUBSCRIBED; } /** * If subscription status has been changed then send email to the customer */ if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) { $sendInformationEmail = true; } if ($status != $this->getStatus()) { $this->setStatusChanged(true); } $this->setStatus($status); if (!$this->getId()) { $storeId = $customerData->getStoreId(); if ($customerData->getStoreId() == 0) { $storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId(); } $this->setStoreId($storeId)->setCustomerId($customerData->getId())->setEmail($customerData->getEmail()); } else { $this->setStoreId($customerData->getStoreId())->setEmail($customerData->getEmail()); } $this->save(); $sendSubscription = $sendInformationEmail; if ($sendSubscription === null xor $sendSubscription) { try { if ($this->isStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) { $this->sendUnsubscriptionEmail(); } elseif ($this->isStatusChanged() && $status == self::STATUS_SUBSCRIBED) { $this->sendConfirmationSuccessEmail(); } } catch (MailException $e) { // If we are not able to send a new account email, this should be ignored $this->_logger->critical($e); } } return $this; }