|
10 | 10 |
|
11 | 11 | use Magento\Customer\Api\AddressMetadataInterface; |
12 | 12 | use Magento\Customer\Model\Metadata\Form as CustomerForm; |
| 13 | +use Magento\Framework\App\ObjectManager; |
| 14 | +use Magento\Quote\Model\Quote\Address; |
13 | 15 | use Magento\Quote\Model\Quote\Item; |
14 | 16 |
|
15 | 17 | /** |
@@ -323,7 +325,7 @@ public function __construct( |
323 | 325 | $this->dataObjectHelper = $dataObjectHelper; |
324 | 326 | $this->orderManagement = $orderManagement; |
325 | 327 | $this->quoteFactory = $quoteFactory; |
326 | | - $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() |
| 328 | + $this->serializer = $serializer ?: ObjectManager::getInstance() |
327 | 329 | ->get(\Magento\Framework\Serialize\Serializer\Json::class); |
328 | 330 | parent::__construct($data); |
329 | 331 | } |
@@ -1449,32 +1451,36 @@ public function getBillingAddress() |
1449 | 1451 | */ |
1450 | 1452 | public function setBillingAddress($address) |
1451 | 1453 | { |
1452 | | - if (is_array($address)) { |
1453 | | - $billingAddress = $this->_objectManager->create( |
1454 | | - \Magento\Quote\Model\Quote\Address::class |
1455 | | - )->setData( |
1456 | | - $address |
1457 | | - )->setAddressType( |
1458 | | - \Magento\Quote\Model\Quote\Address::TYPE_BILLING |
1459 | | - ); |
1460 | | - $this->_setQuoteAddress($billingAddress, $address); |
1461 | | - /** |
1462 | | - * save_in_address_book is not a valid attribute and is filtered out by _setQuoteAddress, |
1463 | | - * that is why it should be added after _setQuoteAddress call |
1464 | | - */ |
1465 | | - $saveInAddressBook = (int)(!empty($address['save_in_address_book'])); |
1466 | | - $billingAddress->setData('save_in_address_book', $saveInAddressBook); |
1467 | | - |
1468 | | - if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) { |
1469 | | - $shippingAddress = clone $billingAddress; |
1470 | | - $shippingAddress->setSameAsBilling(true); |
1471 | | - $shippingAddress->setSaveInAddressBook(false); |
1472 | | - $address['save_in_address_book'] = 0; |
1473 | | - $this->setShippingAddress($address); |
1474 | | - } |
| 1454 | + if (!is_array($address)) { |
| 1455 | + return $this; |
| 1456 | + } |
| 1457 | + |
| 1458 | + $billingAddress = $this->_objectManager->create(Address::class) |
| 1459 | + ->setData($address) |
| 1460 | + ->setAddressType(Address::TYPE_BILLING); |
| 1461 | + |
| 1462 | + $this->_setQuoteAddress($billingAddress, $address); |
| 1463 | + |
| 1464 | + /** |
| 1465 | + * save_in_address_book is not a valid attribute and is filtered out by _setQuoteAddress, |
| 1466 | + * that is why it should be added after _setQuoteAddress call |
| 1467 | + */ |
| 1468 | + $saveInAddressBook = (int)(!empty($address['save_in_address_book'])); |
| 1469 | + $billingAddress->setData('save_in_address_book', $saveInAddressBook); |
| 1470 | + |
| 1471 | + $quote = $this->getQuote(); |
| 1472 | + if (!$quote->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) { |
| 1473 | + $address['save_in_address_book'] = 0; |
| 1474 | + $this->setShippingAddress($address); |
| 1475 | + } |
1475 | 1476 |
|
1476 | | - $this->getQuote()->setBillingAddress($billingAddress); |
| 1477 | + // not assigned billing address should be saved as new |
| 1478 | + // but if quote already has the billing address it won't be overridden |
| 1479 | + if (empty($billingAddress->getCustomerAddressId())) { |
| 1480 | + $billingAddress->setCustomerAddressId(null); |
| 1481 | + $quote->getBillingAddress()->setCustomerAddressId(null); |
1477 | 1482 | } |
| 1483 | + $quote->setBillingAddress($billingAddress); |
1478 | 1484 |
|
1479 | 1485 | return $this; |
1480 | 1486 | } |
@@ -1775,6 +1781,7 @@ public function _prepareCustomer() |
1775 | 1781 | $address = $this->getShippingAddress()->setCustomerId($this->getQuote()->getCustomer()->getId()); |
1776 | 1782 | $this->setShippingAddress($address); |
1777 | 1783 | } |
| 1784 | + $this->getBillingAddress()->setCustomerId($customer->getId()); |
1778 | 1785 | $this->getQuote()->updateCustomerData($this->getQuote()->getCustomer()); |
1779 | 1786 |
|
1780 | 1787 | $customer = $this->getQuote()->getCustomer(); |
|
0 commit comments