public function testPopulateBeforeSaveData() { /** Preconditions */ $customerId = 1; $customerAddressId = 1; $this->_address->setQuote($this->_quote); $this->assertNotEquals($customerId, $this->_address->getCustomerId(), "Precondition failed: Customer ID was not set."); $this->assertNotEquals(1, $this->_address->getQuoteId(), "Precondition failed: Quote ID was not set."); $this->assertNotEquals($customerAddressId, $this->_address->getCustomerAddressId(), "Precondition failed: Customer address ID was not set."); /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $addressBuilder */ $addressBuilder = Bootstrap::getObjectManager()->create('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder'); $customerAddressData = $addressBuilder->setId($customerAddressId)->create(); $this->_address->setCustomerAddressData($customerAddressData); $this->_address->save(); $this->assertEquals($customerId, $this->_address->getCustomerId()); $this->assertEquals($this->_quote->getId(), $this->_address->getQuoteId()); $this->assertEquals($customerAddressId, $this->_address->getCustomerAddressId()); }
/** * @param Address $address * @return string */ public function getEditBillingAddressUrl($address) { return $this->getUrl('*/checkout_address/editBilling', array('id' => $address->getCustomerAddressId())); }
/** * 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); }
/** * Convert quote address to order address * * @param \Magento\Sales\Model\Quote\Address $address * @return \Magento\Sales\Model\Order\Address */ public function addressToOrderAddress(\Magento\Sales\Model\Quote\Address $address) { $orderAddress = $this->_orderAddressFactory->create()->setStoreId($address->getStoreId())->setAddressType($address->getAddressType())->setCustomerId($address->getCustomerId())->setCustomerAddressId($address->getCustomerAddressId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_address', 'to_order_address', $address, $orderAddress); $this->_eventManager->dispatch('sales_convert_quote_address_to_order_address', array('address' => $address, 'order_address' => $orderAddress)); return $orderAddress; }