コード例 #1
0
 /**
  * Copies a quote address to a customer address
  *
  * Ths function serves to create a customer address from a quote address because when a user is logged in and they
  * go to checkout, if they select an existing customer address the checkout module will retrieve that customer
  * address to set as the quote address. Rather that update the quote address every time at checkout, this module
  * will update the customer address (if the user selects the valid address) so that customer address only needs
  * to be validated once. To do this, the quote address must be converted to a customer address so the validated
  * address can be saved to the quote address.
  *
  * @param QuoteAddressInterface $quoteAddress
  * @param CustomerAddressInterface $customerAddress
  * @return null|CustomerAddressInterface
  */
 public function copyQuoteAddressToCustomerAddress(QuoteAddressInterface $quoteAddress, CustomerAddressInterface $customerAddress)
 {
     $customerAddress->setRegionId($quoteAddress->getRegionId());
     $customerAddress->setCountryId($quoteAddress->getCountryId());
     $customerAddress->setStreet($quoteAddress->getStreet());
     $customerAddress->setPostcode($quoteAddress->getPostcode());
     $customerAddress->setCity($quoteAddress->getCity());
     $customerAddressData = $this->getCustomerAddressData($customerAddress);
     $customerAddressDataWithRegion = [];
     $customerAddressDataWithRegion['region']['region'] = $quoteAddress->getRegion();
     $customerAddressDataWithRegion['region']['region_code'] = $quoteAddress->getRegionCode();
     if ($customerAddressData['region_id']) {
         $customerAddressDataWithRegion['region']['region_id'] = $quoteAddress->getRegionId();
     }
     $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion);
     // Per Github issue #4, the \Magento\Framework\Api\DataObjectHelper::_setDataValues method chokes on the
     // custom_attributes array, so remove it and set it after the data has been mapped
     $customAttributes = false;
     if (isset($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]) && is_array($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
         $customAttributes = $customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES];
         unset($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]);
     }
     $addressDataObject = $this->customerAddressFactory->create();
     $this->dataObjectHelper->populateWithArray($addressDataObject, $customerAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface');
     if ($customAttributes) {
         $addressDataObject->setCustomAttributes($customAttributes);
     }
     return $addressDataObject;
 }