/** * Make sure customer is valid, if logged in * * By default will add error messages and redirect to customer edit form * * @param bool $redirect - stop dispatch and redirect? * @param bool $addErrors - add error messages? * @return bool */ protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true) { try { $customerId = $this->_customerSession->getCustomerId(); $customer = $this->_customerAccountService->getCustomer($customerId); } catch (NoSuchEntityException $e) { return true; } if (isset($customer)) { $validationResult = $this->_customerAccountService->validateCustomerData($customer, $this->_customerMetadataService->getAllCustomerAttributeMetadata()); if (!$validationResult->isValid()) { if ($addErrors) { foreach ($validationResult->getMessages() as $error) { $this->messageManager->addError($error); } } if ($redirect) { $this->_redirect('customer/account/edit'); $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true); } return false; } } return true; }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomerData(); $customerData = \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(CustomerMetadata::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, array()); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return array('error' => -1, 'message' => implode(', ', $customerErrors)); } if (!$isCustomerNew) { return true; } $this->_customerBuilder->populateWithArray($customerData); $customer = $this->_customerBuilder->create(); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->_customerAccountService->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $this->_customerBuilder->populate($customer); $this->_customerBuilder->setGroupId(CustomerGroupServiceInterface::NOT_LOGGED_IN_ID); $customer = $this->_customerBuilder->create(); } //validate customer $attributes = $customerForm->getAllowedAttributes(); $result = $this->_customerAccountService->validateCustomerData($customer, $attributes); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer), $quote); return true; }