Ejemplo n.º 1
0
 /**
  * @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());
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * @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 '';
 }
Ejemplo n.º 4
0
 /**
  * Create customerAddressDataObject and save it in the Model\Quote so that it can be used to persist later.
  *
  * @param CustomerDataObject $customerDataObject
  * @param \Magento\Sales\Model\Quote\Address $quoteCustomerAddress
  * @return void
  * @throws \InvalidArgumentException
  */
 protected function _prepareCustomerAddress($customerDataObject, $quoteCustomerAddress)
 {
     // Possible that customerId is null for new customers
     $customerId = $customerDataObject->getId();
     $quoteCustomerAddress->setCustomerId($customerId);
     $customerAddressDataObject = $quoteCustomerAddress->exportCustomerAddressData();
     $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId();
     $addressType = $quoteCustomerAddress->getAddressType();
     if ($quoteAddressId) {
         /** Update existing address */
         $existingAddressDataObject = $this->_customerAddressService->getAddress($quoteAddressId);
         /** Update customer address data */
         $customerAddressDataObject = $this->_customerAddressBuilder->mergeDataObjects($existingAddressDataObject, $customerAddressDataObject);
     } elseif ($addressType == CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING) {
         try {
             $billingAddressDataObject = $this->_customerAddressService->getDefaultBillingAddress($customerId);
         } catch (\Exception $e) {
             /** Billing address does not exist. */
         }
         $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling();
         if (isset($billingAddressDataObject) && $isShippingAsBilling) {
             /** Set existing billing address as default shipping */
             $customerAddressDataObject = $this->_customerAddressBuilder->populate($billingAddressDataObject)->setDefaultShipping(true)->create();
         }
     }
     switch ($addressType) {
         case CustomerAddressDataObject::ADDRESS_TYPE_BILLING:
             if (is_null($customerDataObject->getDefaultBilling())) {
                 $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultBilling(true)->create();
             }
             break;
         case CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING:
             if (is_null($customerDataObject->getDefaultShipping())) {
                 $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultShipping(true)->create();
             }
             break;
         default:
             throw new \InvalidArgumentException('Customer address type is invalid.');
     }
     $this->getQuote()->addCustomerAddressData($customerAddressDataObject);
 }
Ejemplo n.º 5
0
 /**
  * Returns default billing address form current customer
  *
  * @return Address|null
  */
 public function getDefaultBillingAddress()
 {
     return $this->customerAddressService->getDefaultBillingAddress($this->currentCustomer->getCustomerId());
 }
Ejemplo n.º 6
0
 /**
  * Retrieve customer default billing address
  *
  * @return \Magento\Customer\Service\V1\Data\Address|null
  */
 public function getCustomerDefaultBillingAddress()
 {
     $address = $this->getData('customer_default_billing_address');
     if (is_null($address)) {
         $customerId = $this->getCustomer()->getId();
         $address = $this->_customerAddressService->getDefaultBillingAddress($customerId);
         if (!$address) {
             /** Default billing 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_billing_address', $address);
     }
     return $address;
 }
Ejemplo n.º 7
0
 /**
  * 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();
 }