/** * @return \Magento\Customer\Service\V1\Data\Customer */ public function getCustomer() { if (!$this->customer) { $this->customer = $this->customerBuilder->populateWithArray($this->_backendSession->getCustomerData()['account'])->create(); } return $this->customer; }
/** * @param string $formCode * @param RequestInterface $request * @return \Magento\Customer\Service\V1\Data\Customer */ public function extract($formCode, RequestInterface $request) { $customerForm = $this->formFactory->create('customer', $formCode); $allowedAttributes = $customerForm->getAllowedAttributes(); $isGroupIdEmpty = true; $customerData = array(); foreach ($allowedAttributes as $attribute) { // confirmation in request param is the repeated password, not a confirmation code. if ($attribute === 'confirmation') { continue; } $attributeCode = $attribute->getAttributeCode(); if ($attributeCode == 'group_id') { $isGroupIdEmpty = false; } $customerData[$attributeCode] = $request->getParam($attributeCode); } $this->customerBuilder->populateWithArray($customerData); $store = $this->storeManager->getStore(); if ($isGroupIdEmpty) { $this->customerBuilder->setGroupId($this->groupService->getDefaultGroup($store->getId())->getId()); } $this->customerBuilder->setWebsiteId($store->getWebsiteId()); $this->customerBuilder->setStoreId($store->getId()); return $this->customerBuilder->create(); }
/** * {@inheritdoc} */ protected function _setDataValues(array $data) { $newData = array(); if (isset($data[CustomerDetails::KEY_CUSTOMER])) { $newData[CustomerDetails::KEY_CUSTOMER] = $this->_customerBuilder->populateWithArray($data[CustomerDetails::KEY_CUSTOMER])->create(); } if (isset($data[CustomerDetails::KEY_ADDRESSES])) { $newData[CustomerDetails::KEY_ADDRESSES] = array(); $addresses = $data[CustomerDetails::KEY_ADDRESSES]; foreach ($addresses as $address) { $newData[CustomerDetails::KEY_ADDRESSES][] = $this->_addressBuilder->populateWithArray($address)->create(); } } return parent::_setDataValues($newData); }
public function testToFlatArray() { $customerData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'unknown_key' => 'Golden Necklace', 'warehouse_zip' => '78777', 'warehouse_alternate' => '90051'); $expectedCustomerData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'warehouse_zip' => '78777', 'warehouse_alternate' => '90051'); $customer = $this->_customerBuilder->populateWithArray($customerData)->create(); $this->assertEquals($expectedCustomerData, \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customer)); }
public function testPopulateFromCustomerIdInArray() { $customer = $this->_customerBuilder->populateWithArray(array(Customer::FIRSTNAME => self::FIRSTNAME, Customer::LASTNAME => self::LASTNAME, Customer::EMAIL => self::EMAIL, Customer::ID => self::ID))->create(); $this->assertEquals(self::FIRSTNAME, $customer->getFirstname()); $this->assertEquals(self::LASTNAME, $customer->getLastname()); $this->assertEquals(self::EMAIL, $customer->getEmail()); $this->assertEquals(self::ID, $customer->getId()); }
/** * Obtain customer data from session and create customer object * * @return \Magento\Customer\Service\V1\Data\Customer */ protected function _getCustomerDataObject() { if (is_null($this->_customerDataObject)) { $customerData = $this->_backendSession->getCustomerData(); $accountData = isset($customerData['account']) ? $customerData['account'] : array(); $this->_customerDataObject = $this->_customerBuilder->populateWithArray($accountData)->create(); } return $this->_customerDataObject; }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @expectedException \Magento\Framework\Exception\StateException */ public function testUpdateCustomerDetailsByEmailWithException() { $customerId = 1; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => 'fname', 'id' => 1234567]); $this->_customerBuilder->populateWithArray($customerData); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create()); }
/** * Get customer data from session or service. * * @param int|null $customerId possible customer ID from DB * @return Customer * @throws NoSuchEntityException */ protected function getCustomer($customerId) { $customerData = $this->_backendSession->getCustomerData(); if (!empty($customerData['account'])) { return $this->_customerBuilder->populateWithArray($customerData['account'])->create(); } elseif ($customerId) { return $this->_customerAccountService->getCustomer($customerId); } else { return $this->_customerBuilder->create(); } }
public function connectByCreatingAccount($facebookId, $token, $email, $firstName, $lastName) { $customerDetails = array('firstname' => $firstName, 'lastname' => $lastName, 'email' => $email, 'sendemail' => 0, 'confirmation' => 0, 'custom_attributes' => array(array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_fid', \Magento\Framework\Service\Data\AttributeValue::VALUE => $facebookId), array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_ftoken', \Magento\Framework\Service\Data\AttributeValue::VALUE => serialize($token)))); $customer = $this->_customerBuilder->populateWithArray($customerDetails)->create(); // Save customer $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses(null)->create(); $customerDataObject = $this->_customerAccountService->createCustomer($customerDetails); /* @var $customer \Magento\Customer\Service\V1\Data\Customer */ // Convert data object to customer model $customer = $this->_converter->createCustomerModel($customerDataObject); /* @var $customer \Magento\Customer\Model\Customer */ $customer->sendNewAccountEmail('confirmed', ''); $this->_customerSession->setCustomerAsLoggedIn($customer); }
/** * Loads the values from a customer model * * @param Customer $customerModel * @return CustomerDataObjectBuilder */ protected function _populateBuilderWithAttributes(Customer $customerModel) { $attributes = array(); foreach ($customerModel->getAttributes() as $attribute) { $attrCode = $attribute->getAttributeCode(); $value = $customerModel->getDataUsingMethod($attrCode); $value = $value ? $value : $customerModel->getData($attrCode); if (null !== $value) { if ($attrCode == 'entity_id') { $attributes[CustomerDataObject::ID] = $value; } else { $attributes[$attrCode] = $value; } } } return $this->_customerBuilder->populateWithArray($attributes); }
/** * Prepare quote for customer registration and customer order submit * and restore magento customer data from quote * * @return void */ protected function _prepareNewCustomerQuote() { $quote = $this->_quote; $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $billingAddressBuilder */ $billingAddressBuilder = $this->_addressBuilderFactory->create(); $customerBilling = $billingAddressBuilder->populate($billing->exportCustomerAddressData())->setDefaultBilling(true); if ($shipping && !$shipping->getSameAsBilling()) { /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $shippingAddressBuilder */ $shippingAddressBuilder = $this->_addressBuilderFactory->create(); $customerShipping = $shippingAddressBuilder->populate($shipping->exportCustomerAddressData())->setDefaultShipping(true)->create(); $shipping->setCustomerAddressData($customerShipping); } elseif ($shipping) { $customerBilling->setDefaultShipping(true); } $customerBilling = $customerBilling->create(); $billing->setCustomerAddressData($customerBilling); /** * @todo integration with dynamic attributes customer_dob, customer_taxvat, customer_gender */ if ($quote->getCustomerDob() && !$billing->getCustomerDob()) { $billing->setCustomerDob($quote->getCustomerDob()); } if ($quote->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) { $billing->setCustomerTaxvat($quote->getCustomerTaxvat()); } if ($quote->getCustomerGender() && !$billing->getCustomerGender()) { $billing->setCustomerGender($quote->getCustomerGender()); } $customerData = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing); $customer = $this->_customerBuilder->populateWithArray($customerData); $customer->setEmail($quote->getCustomerEmail()); $customer->setPrefix($quote->getCustomerPrefix()); $customer->setFirstname($quote->getCustomerFirstname()); $customer->setMiddlename($quote->getCustomerMiddlename()); $customer->setLastname($quote->getCustomerLastname()); $customer->setSuffix($quote->getCustomerSuffix()); $quote->setCustomerData($customer->create())->addCustomerAddressData($customerBilling); if (isset($customerShipping)) { $quote->addCustomerAddressData($customerShipping); } }
/** * @expectedException \Magento\Customer\Exception * @expectedExceptionMessage exception message */ public function testCreateCustomerWithPasswordHashException() { $storeId = 5; $customerData = array('id' => self::ID, 'email' => self::EMAIL, 'firstname' => self::FIRSTNAME, 'lastname' => self::LASTNAME, 'store_id' => $storeId, 'website_id' => self::WEBSITE_ID); $this->_customerBuilder->populateWithArray($customerData); $customerEntity = $this->_customerBuilder->create(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerEntity)->create(); $this->_storeManagerMock->expects($this->once())->method('getStores')->will($this->returnValue(array())); $this->_converter = $this->getMock('Magento\\Customer\\Model\\Converter', array('getCustomerModel', 'createCustomerModel'), array(), '', false); $this->_converter->expects($this->once())->method('getCustomerModel')->will($this->returnValue($this->_customerModelMock)); $this->_converter->expects($this->once())->method('createCustomerModel')->will($this->throwException(new \Magento\Customer\Exception('exception message', 0))); $customerService = $this->_createService(); $customerService->createCustomerWithPasswordHash($customerDetails, '', ''); }
/** * @return \Magento\Customer\Service\V1\Data\Customer */ protected function _getCustomer() { return $this->_customerBuilder->populateWithArray($this->_backendSession->getCustomerData()['account'])->create(); }
/** * Initialize form object * * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initForm() { $customerData = $this->_backendSession->getCustomerData(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('address_fieldset', array('legend' => __("Edit Customer's Address"))); $account = $customerData['account']; $this->_addressBuilder->populateWithArray(array()); if (!empty($account) && isset($account['store_id'])) { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry($this->_storeManager->getStore($account['store_id']))); } else { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry()); } $address = $this->_addressBuilder->create(); $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address)); $attributes = $addressForm->getAttributes(); if (isset($attributes['street'])) { if ($attributes['street']->getMultilineCount() <= 0) { $attributes['street'] = $this->_attributeMetadataBuilder->populate($attributes['street'])->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT)->create(); } } foreach ($attributes as $key => $attribute) { $attributes[$key] = $this->_attributeMetadataBuilder->populate($attribute)->setFrontendLabel(__($attribute->getFrontendLabel()))->setVisible(false)->create(); } $this->_setFieldset($attributes, $fieldset); $regionElement = $form->getElement('region'); if ($regionElement) { $regionElement->setRenderer($this->_regionFactory->create()); } $regionElement = $form->getElement('region_id'); if ($regionElement) { $regionElement->setNoDisplay(true); } $country = $form->getElement('country_id'); if ($country) { $country->addClass('countries'); } if ($this->isReadonly()) { foreach ($this->_metadataService->getAllAddressAttributeMetadata() as $attribute) { $element = $form->getElement($attribute->getAttributeCode()); if ($element) { $element->setReadonly(true, true); } } } $customerStoreId = null; if (!empty($account) && isset($account['id']) && isset($account['website_id'])) { $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId(); } $prefixElement = $form->getElement('prefix'); if ($prefixElement) { $prefixOptions = $this->_customerHelper->getNamePrefixOptions($customerStoreId); if (!empty($prefixOptions)) { $fieldset->removeField($prefixElement->getId()); $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^'); $prefixField->setValues($prefixOptions); } } $suffixElement = $form->getElement('suffix'); if ($suffixElement) { $suffixOptions = $this->_customerHelper->getNameSuffixOptions($customerStoreId); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); } } $this->assign('customer', $this->_customerBuilder->populateWithArray($account)->create()); $addressCollection = array(); foreach ($customerData['address'] as $key => $addressData) { $addressCollection[$key] = $this->_addressBuilder->populateWithArray($addressData)->create(); } $this->assign('addressCollection', $addressCollection); $form->setValues(AddressConverter::toFlatArray($address)); $this->setForm($form); return $this; }
/** * 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; }
/** * {@inheritdoc} */ protected function _afterSave() { $customerData = (array) $this->getData(); $customerData[CustomerData::ID] = $this->getId(); $dataObject = $this->_customerDataBuilder->populateWithArray($customerData)->create(); $customerOrigData = (array) $this->getOrigData(); $customerOrigData[CustomerData::ID] = $this->getId(); $origDataObject = $this->_customerDataBuilder->populateWithArray($customerOrigData)->create(); $this->_eventManager->dispatch('customer_save_after_data_object', array('customer_data_object' => $dataObject, 'orig_customer_data_object' => $origDataObject)); return parent::_afterSave(); }