Example #1
0
 /**
  * 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;
 }