/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Sales\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customerData = $quote->getCustomerData(); $storeId = $customerData->getStoreId(); if ($customerData->getCustomAttribute('disable_auto_group_change') && $customerData->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerHelper->isCountryInEU($customerCountryCode)) { $groupId = $customerData->getId() ? $this->customerHelper->getDefaultCustomerGroupId($storeId) : \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID; } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerHelper->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customerData = $this->customerBuilder->mergeDataObjectWithArray($customerData, array('group_id' => $groupId)); $quote->setCustomerData($customerData); } }
/** * Forgot customer account information page * * @return void */ public function execute() { $this->_view->loadLayout(); $this->_view->getLayout()->initMessages(); $block = $this->_view->getLayout()->getBlock('customer_edit'); if ($block) { $block->setRefererUrl($this->_redirect->getRefererUrl()); } $data = $this->_getSession()->getCustomerFormData(true); $customerId = $this->_getSession()->getCustomerId(); $customerDataObject = $this->_customerAccountService->getCustomer($customerId); if (!empty($data)) { $customerDataObject = $this->_customerBuilder->mergeDataObjectWithArray($customerDataObject, $data); } $this->_getSession()->setCustomerData($customerDataObject); $this->_getSession()->setChangePassword($this->getRequest()->getParam('changepass') == 1); $this->_view->getLayout()->getBlock('head')->setTitle(__('Account Information')); $this->_view->getLayout()->getBlock('messages')->setEscapeMessageFlag(true); $this->_view->renderLayout(); }
public function testMergeDataObjectWithArrayCustomData() { $customerData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'unknown_key' => 'Golden Necklace', Customer::CUSTOM_ATTRIBUTES_KEY => ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78777'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]); $customer = $this->_customerBuilder->populateWithArray($customerData)->create(); $customer2 = $this->_customerBuilder->mergeDataObjectWithArray($customer, ['lastname' => 'Johnson', 'unknown_key' => 'Golden Necklace', Customer::CUSTOM_ATTRIBUTES_KEY => ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78666'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]]); $expectedData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Johnson', Customer::CUSTOM_ATTRIBUTES_KEY => ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78666'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]); $this->assertEquals('78666', $customer2->getCustomAttribute('warehouse_zip')->getValue()); $this->assertEquals('90051', $customer2->getCustomAttribute('warehouse_alternate')->getValue()); foreach ($customer2->getCustomAttributes() as $customAttribute) { $this->assertEquals($expectedData[Customer::CUSTOM_ATTRIBUTES_KEY][$customAttribute->getAttributeCode()]['value'], $customAttribute->getValue()); } $this->assertEquals($expectedData, $customer2->__toArray()); }
/** * @magentoDataFixture Magento/Sales/_files/quote.php */ public function testSubmitOrderExistingCustomer() { $this->_prepareQuote(false); $customerDetails = $this->_customerDetailsBuilder->setCustomer($this->getSampleCustomerEntity())->setAddresses($this->getSampleAddressEntity())->create(); $customerData = $this->_customerAccountService->createCustomer($customerDetails, 'password'); $existingCustomerId = $customerData->getId(); $customerData = $this->_customerBuilder->mergeDataObjectWithArray($customerData, array(CustomerData::EMAIL => '*****@*****.**')); $addresses = $this->_customerAddressService->getAddresses($existingCustomerId); $this->_serviceQuote->getQuote()->setCustomerData($customerData); $this->_serviceQuote->getQuote()->setCustomerAddressData($addresses); $this->_serviceQuote->submitOrderWithDataObject(); $customerId = $this->_serviceQuote->getQuote()->getCustomerData()->getId(); $this->assertNotNull($customerId); //Make sure no new customer is created $this->assertEquals($existingCustomerId, $customerId); $customerData = $this->_customerAccountService->getCustomer($existingCustomerId); $this->assertEquals('*****@*****.**', $customerData->getEmail()); }
/** * Set and validate Customer data. Return the updated Data Object merged with the account data * * @param CustomerDataObject $customerDataObject * @return CustomerDataObject */ protected function _validateCustomerData(CustomerDataObject $customerDataObject) { $form = $this->_createCustomerForm($customerDataObject); // emulate request $request = $form->prepareRequest(array('order' => $this->getData())); $data = $form->extractData($request, 'order/account'); if ($this->getIsValidate()) { $errors = $form->validateData($data); if (is_array($errors)) { foreach ($errors as $error) { $this->_errors[] = $error; } } } $data = $form->restoreData($data); foreach ($data as $key => $value) { if (!is_null($value)) { unset($data[$key]); } } return $this->_customerBuilder->mergeDataObjectWithArray($customerDataObject, $data); }
/** * Prepare quote for customer registration and customer order submit * * @return void */ protected function _prepareNewCustomerQuote() { $quote = $this->getQuote(); $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customerData = $quote->getCustomerData(); $customerBillingData = $billing->exportCustomerAddressData(); $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultBilling(true)->create(); if ($shipping) { if (!$shipping->getSameAsBilling()) { $customerShippingData = $shipping->exportCustomerAddressData(); $customerShippingData = $this->_addressBuilder->populate($customerShippingData)->setDefaultShipping(true)->create(); $shipping->setCustomerAddressData($customerShippingData); // Add shipping address to quote since customer Data Object does not hold address information $quote->addCustomerAddressData($customerShippingData); } else { $shipping->setCustomerAddressData($customerBillingData); $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultShipping(true)->create(); } } else { $customerBillingData = $this->_addressBuilder->populate($customerBillingData)->setDefaultShipping(true)->create(); } $billing->setCustomerAddressData($customerBillingData); $dataArray = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_quote', 'to_customer', $quote); $customerData = $this->_customerBuilder->mergeDataObjectWithArray($customerData, $dataArray); $quote->setCustomerData($customerData)->setCustomerId(true); // TODO : Eventually need to remove this legacy hack // Add billing address to quote since customer Data Object does not hold address information $quote->addCustomerAddressData($customerBillingData); }