/** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_address.php * @magentoAppIsolation enabled */ public function testSaveSeveralNewAddressesDifferentDefaults() { $addressTwoBuilder = $this->_createSecondAddressBuilder(); $addressTwo = $addressTwoBuilder->create(); $addressThreeBuilder = $this->_addressBuilder->populate($addressTwo); $addressThreeBuilder->setDefaultBilling(true); $addressThree = $addressThreeBuilder->create(); $defaultShippingBuilder = $this->_addressBuilder->populate($addressTwo); $defaultShippingBuilder->setFirstname('Shippy')->setLastname('McShippington')->setDefaultBilling(false)->setDefaultShipping(true); $defaultShipping = $defaultShippingBuilder->create(); $defaultBillingBuilder = $this->_addressBuilder->populate($addressTwo); $defaultBillingBuilder->setFirstname('Billy')->setLastname('McBillington')->setDefaultBilling(true)->setDefaultShipping(false); $defaultBilling = $defaultBillingBuilder->create(); $customerId = 1; $this->_service->saveAddresses($customerId, array($addressTwo, $addressThree, $defaultShipping, $defaultBilling)); $addresses = $this->_service->getAddresses($customerId); $this->assertEquals(5, count($addresses)); $addresses = array($this->_service->getDefaultBillingAddress($customerId), $this->_service->getDefaultShippingAddress($customerId)); $this->assertNotEquals($addresses[0], $addresses[1]); $this->assertTrue($addresses[0]->isDefaultBilling()); $this->assertTrue($addresses[1]->isDefaultShipping()); $expectedDfltShipBuilder = $this->_addressBuilder->populate($defaultShipping); $expectedDfltShipBuilder->setId($addresses[1]->getId()); $expectedDfltShip = $expectedDfltShipBuilder->create(); $expectedDfltBillBuilder = $this->_addressBuilder->populate($defaultBilling); $expectedDfltBillBuilder->setId($addresses[0]->getId()); $expectedDfltBill = $expectedDfltBillBuilder->create(); $this->_assertAddressAndRegionArrayEquals($expectedDfltShip->__toArray(), $addresses[1]->__toArray()); $this->_assertAddressAndRegionArrayEquals($expectedDfltBill->__toArray(), $addresses[0]->__toArray()); }
/** * Retrieve the customer's primary addresses (i.e. default billing and shipping). * * @return \Magento\Customer\Service\V1\Data\Address[]|bool */ public function getPrimaryAddresses() { $addresses = array(); $customerId = $this->getCustomer()->getId(); if ($defaultBilling = $this->_addressService->getDefaultBillingAddress($customerId)) { $addresses[] = $defaultBilling; } if ($defaultShipping = $this->_addressService->getDefaultShippingAddress($customerId)) { if ($defaultBilling) { if ($defaultBilling->getId() != $defaultShipping->getId()) { $addresses[] = $defaultShipping; } } else { $addresses[] = $defaultShipping; } } return empty($addresses) ? false : $addresses; }
/** * @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/')); }
/** * @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 ''; }
/** * Returns default shipping address for current customer * * @return Address|null */ public function getDefaultShippingAddress() { return $this->customerAddressService->getDefaultShippingAddress($this->currentCustomer->getCustomerId()); }
/** * 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; }
/** * Returns true if shipping address is same as billing or it is undefined * * @return bool */ protected function _isDefaultShippingNullOrSameAsBillingAddress() { $customerData = $this->getQuote()->getCustomerData(); $customerId = $customerData->getId(); $defaultBillingAddress = null; $defaultShippingAddress = null; if ($customerId) { /* we should load data from the service once customer is saved */ $defaultBillingAddress = $this->_customerAdressService->getDefaultBillingAddress($customerId); $defaultShippingAddress = $this->_customerAdressService->getDefaultShippingAddress($customerId); } else { /* we should load data from the quote if customer is not saved yet */ $defaultBillingAddress = $customerData->getDefaultBilling(); $defaultShippingAddress = $customerData->getDefaultShipping(); } return !$defaultShippingAddress || $defaultBillingAddress && $defaultShippingAddress && $defaultBillingAddress->getId() == $defaultShippingAddress->getId(); }