/** * @return void */ public function execute() { /** * if we create first address we need reset emd init checkout */ $customerId = $this->_getCheckout()->getCustomer()->getId(); if (count($this->_customerAddressService->getAddresses($customerId)) == 1) { $this->_getCheckout()->reset(); } $this->_redirect('*/checkout/addresses'); }
/** * Get a list of current customer addresses. * * @return \Magento\Customer\Service\V1\Data\Address[] */ public function getAddressCollection() { $addresses = $this->getData('address_collection'); if (is_null($addresses)) { try { $addresses = $this->_customerAddressService->getAddresses($this->_multishipping->getCustomer()->getId()); } catch (NoSuchEntityException $e) { return array(); } $this->setData('address_collection', $addresses); } return $addresses; }
/** * Retrieve current customer address DATA collection. * * @return \Magento\Customer\Service\V1\Data\Address[] */ public function getAddressCollection() { if ($this->getCustomerId()) { return $this->_addressService->getAddresses($this->getCustomerId()); } return array(); }
/** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php * @magentoAppIsolation enabled */ public function testSaveAddressesNoAddresses() { $addressIds = $this->_service->saveAddresses(1, array()); $this->assertEmpty($addressIds); $customerId = 1; $addresses = $this->_service->getAddresses($customerId); $this->assertEquals($this->_expectedAddresses, $addresses); }
/** * Put existing customer data into the backend session */ protected function setupExistingCustomerData() { /** @var Customer $customer */ $customer = $this->_customerAccountService->getCustomer(1); $this->_customerData = array('customer_id' => $customer->getId(), 'account' => \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customer)); $this->_customerData['account']['id'] = $customer->getId(); /** @var Address[] $addresses */ $addresses = $this->_addressService->getAddresses(1); foreach ($addresses as $addressData) { $this->_customerData['address'][$addressData->getId()] = AddressConverter::toFlatArray($addressData); $this->_customerData['address'][$addressData->getId()]['id'] = $addressData->getId(); } $this->_backendSession->setCustomerData($this->_customerData); }
/** * @return \Magento\Customer\Service\V1\Data\Address[]|bool */ public function getAdditionalAddresses() { try { $addresses = $this->_addressService->getAddresses($this->currentCustomer->getCustomerId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return false; } $primaryAddressIds = array($this->getDefaultBilling(), $this->getDefaultShipping()); foreach ($addresses as $address) { if (!in_array($address->getId(), $primaryAddressIds)) { $additional[] = $address; } } return empty($additional) ? false : $additional; }
/** * @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()); }
/** * Retrieve options for addresses dropdown * * @return array */ public function getAddressOptions() { $options = $this->getData('address_options'); if (is_null($options)) { $options = []; $addresses = []; try { $addresses = $this->_customerAddressService->getAddresses($this->getCustomerId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** Customer does not exist */ } /** @var \Magento\Customer\Service\V1\Data\Address $address */ foreach ($addresses as $address) { $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address)); $options[] = ['value' => $address->getId(), 'label' => $label]; } $this->setData('address_options', $options); } return $options; }
/** * @magentoDataFixture Magento/Customer/_files/customer_sample.php */ public function testSaveActionExistingCustomerAndExistingAddressData() { $post = array('customer_id' => '1', 'account' => array('middlename' => 'test middlename', 'group_id' => 1, 'website_id' => 1, 'firstname' => 'test firstname', 'lastname' => 'test lastname', 'email' => '*****@*****.**', 'default_shipping' => '_item1', 'new_password' => 'auto', 'sendemail_store_id' => '1', 'sendemail' => '1'), 'address' => array('1' => array('firstname' => 'update firstname', 'lastname' => 'update lastname', 'street' => array('update street'), 'city' => 'update city', 'country_id' => 'US', 'postcode' => '01001', 'telephone' => '+7000000001'), '_item1' => array('firstname' => 'new firstname', 'lastname' => 'new lastname', 'street' => array('new street'), 'city' => 'new city', 'country_id' => 'US', 'postcode' => '01001', 'telephone' => '+7000000001'), '_template_' => array('firstname' => '', 'lastname' => '', 'street' => array(), 'city' => '', 'country_id' => 'US', 'postcode' => '', 'telephone' => '')), 'subscription' => ''); $this->getRequest()->setPost($post); $this->getRequest()->setParam('customer_id', 1); $this->dispatch('backend/customer/index/save'); /** * Check that success message is set */ $this->assertSessionMessages($this->equalTo(array('You saved the customer.')), \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS); /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = Bootstrap::getObjectManager(); /** * Check that customer id set and addresses saved */ $registry = $objectManager->get('Magento\\Framework\\Registry'); $customerId = $registry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); $customer = $this->customerAccountService->getCustomer($customerId); $this->assertEquals('test firstname', $customer->getFirstname()); /** * Addresses should be removed by \Magento\Customer\Model\Resource\Customer::_saveAddresses during _afterSave * addressOne - updated * addressTwo - removed * addressThree - removed * _item1 - new address */ $addresses = $this->customerAddressService->getAddresses($customerId); $this->assertEquals(2, count($addresses)); $updatedAddress = $this->customerAddressService->getAddress(1); $this->assertEquals('update firstname', $updatedAddress->getFirstname()); $newAddress = $this->customerAddressService->getDefaultShippingAddress($customerId); $this->assertEquals('new firstname', $newAddress->getFirstname()); /** @var \Magento\Newsletter\Model\Subscriber $subscriber */ $subscriber = $objectManager->get('Magento\\Newsletter\\Model\\SubscriberFactory')->create(); $this->assertEmpty($subscriber->getId()); $subscriber->loadByCustomerId($customerId); $this->assertNotEmpty($subscriber->getId()); $this->assertEquals(1, $subscriber->getStatus()); $this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/key/')); }
/** * Get order data jason * * @return string */ public function getOrderDataJson() { $data = array(); if ($this->getCustomerId()) { $data['customer_id'] = $this->getCustomerId(); $data['addresses'] = array(); $addresses = $this->_addressService->getAddresses($this->getCustomerId()); foreach ($addresses as $addressData) { $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($addressData)); $data['addresses'][$addressData->getId()] = $addressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON); } } if (!is_null($this->getStoreId())) { $data['store_id'] = $this->getStoreId(); $currency = $this->_localeCurrency->getCurrency($this->getStore()->getCurrentCurrencyCode()); $symbol = $currency->getSymbol() ? $currency->getSymbol() : $currency->getShortName(); $data['currency_symbol'] = $symbol; $data['shipping_method_reseted'] = !(bool) $this->getQuote()->getShippingAddress()->getShippingMethod(); $data['payment_method'] = $this->getQuote()->getPayment()->getMethod(); } return $this->_jsonEncoder->encode($data); }
/** * @param string $type * @return string */ public function getAddressesHtmlSelect($type) { if ($this->isCustomerLoggedIn()) { $customerId = $this->_getCustomerData()->getId(); $options = array(); try { $addresses = $this->_customerAddressService->getAddresses($customerId); } catch (NoSuchEntityException $e) { $addresses = array(); } foreach ($addresses as $address) { /** @var \Magento\Customer\Service\V1\Data\Address $address */ $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address)); $options[] = array('value' => $address->getId(), 'label' => $label); } $addressId = $this->getAddress()->getCustomerAddressId(); if (empty($addressId)) { try { if ($type == 'billing') { $address = $this->_customerAddressService->getDefaultBillingAddress($customerId); } else { $address = $this->_customerAddressService->getDefaultShippingAddress($customerId); } if ($address) { $addressId = $address->getId(); } } catch (NoSuchEntityException $e) { // Do nothing } } $select = $this->getLayout()->createBlock('Magento\\Framework\\View\\Element\\Html\\Select')->setName($type . '_address_id')->setId($type . '-address-select')->setClass('address-select')->setValue($addressId)->setOptions($options); $select->addOption('', __('New Address')); return $select->getHtml(); } return ''; }
/** * {inheritDoc} */ public function getCustomerDetailsByEmail($customerEmail, $websiteId = null) { $customerData = $this->getCustomerByEmail($customerEmail, $websiteId); return $this->customerDetailsBuilder->setCustomer($customerData)->setAddresses($this->customerAddressService->getAddresses($customerData->getId()))->create(); }
/** * Retrieve the number of addresses associated with the customer given a customer Id. * * @return int */ public function getCustomerAddressCount() { return count($this->_addressService->getAddresses($this->_customerSession->getCustomerId())); }
/** * Returns all addresses for current customer * * @return Address[] */ public function getCustomerAddresses() { return $this->customerAddressService->getAddresses($this->currentCustomer->getCustomerId()); }
/** * Submit the quote. Quote submit process will create the order based on quote data * * @return \Magento\Sales\Model\Order * @throws \Exception */ public function submitOrderWithDataObject() { $this->_deleteNominalItems(); $this->_validate(); $quote = $this->_quote; $isVirtual = $quote->isVirtual(); $transaction = $this->_transactionFactory->create(); $customerData = null; if (!$quote->getCustomerIsGuest()) { $customerData = $quote->getCustomerData(); $addresses = $quote->getCustomerAddressData(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerData)->setAddresses($addresses)->create(); if ($customerData->getId()) { $this->_customerAccountService->updateCustomer($customerDetails); } else { //for new customers $customerData = $this->_customerAccountService->createCustomerWithPasswordHash($customerDetails, $quote->getPasswordHash()); $addresses = $this->_customerAddressService->getAddresses($customerData->getId()); //Update quote address information foreach ($addresses as $address) { if ($address->isDefaultBilling()) { $quote->getBillingAddress()->setCustomerAddressData($address); } else { if ($address->isDefaultShipping()) { $quote->getShippingAddress()->setCustomerAddressData($address); } } } if ($quote->getShippingAddress() && $quote->getShippingAddress()->getSameAsBilling()) { $quote->getShippingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData()); } } $quote->setCustomerData($customerData)->setCustomerAddressData($addresses); } $transaction->addObject($quote); $quote->reserveOrderId(); if ($isVirtual) { $order = $this->_convertor->addressToOrder($quote->getBillingAddress()); } else { $order = $this->_convertor->addressToOrder($quote->getShippingAddress()); } $order->setBillingAddress($this->_convertor->addressToOrderAddress($quote->getBillingAddress())); if ($quote->getBillingAddress()->getCustomerAddressData()) { $order->getBillingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData()); } if (!$isVirtual) { $order->setShippingAddress($this->_convertor->addressToOrderAddress($quote->getShippingAddress())); if ($quote->getShippingAddress()->getCustomerAddressData()) { $order->getShippingAddress()->setCustomerAddressData($quote->getShippingAddress()->getCustomerAddressData()); } } $order->setPayment($this->_convertor->paymentToOrderPayment($quote->getPayment())); foreach ($this->_orderData as $key => $value) { $order->setData($key, $value); } foreach ($quote->getAllItems() as $item) { $orderItem = $this->_convertor->itemToOrderItem($item); if ($item->getParentItem()) { $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); } $order->addItem($orderItem); } if ($customerData) { $order->setCustomerId($customerData->getId()); } $order->setQuote($quote); $transaction->addObject($order); $transaction->addCommitCallback(array($order, 'place')); $transaction->addCommitCallback(array($order, 'save')); /** * We can use configuration data for declare new order status */ $this->_eventManager->dispatch('checkout_type_onepage_save_order', array('order' => $order, 'quote' => $quote)); $this->_eventManager->dispatch('sales_model_service_quote_submit_before', array('order' => $order, 'quote' => $quote)); try { $transaction->save(); $this->_inactivateQuote(); $this->_eventManager->dispatch('sales_model_service_quote_submit_success', array('order' => $order, 'quote' => $quote)); } catch (\Exception $e) { //reset order ID's on exception, because order not saved $order->setId(null); /** @var $item \Magento\Sales\Model\Order\Item */ foreach ($order->getItemsCollection() as $item) { $item->setOrderId(null); $item->setItemId(null); } $this->_eventManager->dispatch('sales_model_service_quote_submit_failure', array('order' => $order, 'quote' => $quote)); throw $e; } $this->_order = $order; return $order; }
/** * Retrieve customer default shipping address * * @return \Magento\Customer\Service\V1\Data\Address|null */ public function getCustomerDefaultShippingAddress() { $address = $this->getData('customer_default_shipping_address'); if (is_null($address)) { $customerId = $this->getCustomer()->getId(); $address = $this->_customerAddressService->getDefaultShippingAddress($customerId); if (!$address) { /** Default shipping address is not available, try to find any customer address */ $allAddresses = $this->_customerAddressService->getAddresses($customerId); $address = count($allAddresses) ? reset($allAddresses) : null; } $this->setData('customer_default_shipping_address', $address); } return $address; }