/** * Put existing customer data into the backend session */ protected function setupExistingCustomerData() { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->_customerRepository->getById(1); $this->_customerData = ['customer_id' => $customer->getId(), 'account' => $this->customerMapper->toFlatArray($customer)]; $this->_customerData['account']['id'] = $customer->getId(); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = $customer->getAddresses(); foreach ($addresses as $addressData) { $this->_customerData['address'][$addressData->getId()] = $this->addressMapper->toFlatArray($addressData); $this->_customerData['address'][$addressData->getId()]['id'] = $addressData->getId(); } $this->_backendSession->setCustomerData($this->_customerData); }
/** * Prepare customer data for order creation. * * Create customer if not created using data from customer form. * Create customer billing/shipping address if necessary using data from customer address forms. * Set customer data to quote. * * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function _prepareCustomer() { if ($this->getQuote()->getCustomerIsGuest()) { return $this; } /** @var $store \Magento\Store\Model\Store */ $store = $this->getSession()->getStore(); $customer = $this->getQuote()->getCustomer(); if ($customer->getId() && !$this->_customerIsInStore($store)) { /** Create a new customer record if it is not available in the specified store */ /** Unset customer ID to ensure that new customer will be created */ $customer->setId(null)->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setCreatedAt(null); $customer = $this->_validateCustomerData($customer); } else { if (!$customer->getId()) { /** Create new customer */ $customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddress(); $customer->setSuffix($customerBillingAddressDataObject->getSuffix())->setFirstname($customerBillingAddressDataObject->getFirstname())->setLastname($customerBillingAddressDataObject->getLastname())->setMiddlename($customerBillingAddressDataObject->getMiddlename())->setPrefix($customerBillingAddressDataObject->getPrefix())->setStoreId($store->getId())->setEmail($this->_getNewCustomerEmail()); $customer = $this->_validateCustomerData($customer); } } $this->getQuote()->setCustomer($customer); if ($this->getBillingAddress()->getSaveInAddressBook()) { $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getBillingAddress()); } if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) { $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getShippingAddress()); } $this->getQuote()->updateCustomerData($this->getQuote()->getCustomer()); $customer = $this->getQuote()->getCustomer(); $origAddresses = $customer->getAddresses(); // save original addresses $customer->setAddresses([]); $customerData = $this->customerMapper->toFlatArray($customer); $customer->setAddresses($origAddresses); // restore original addresses foreach ($this->_createCustomerForm($customer)->getUserAttributes() as $attribute) { if (isset($customerData[$attribute->getAttributeCode()])) { $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode()); $this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]); } } return $this; }
/** * Update customer data * * @param array $data * @return void */ protected function updateCustomer(array $data) { $customer = $this->getCustomer(); $customerData = array_merge($this->customerMapper->toFlatArray($customer), $data); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); }