|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
|
10 | 10 | use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
|
11 | 11 | use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
|
12 | 12 | use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
|
| 13 | +use Magento\Customer\Api\Data\AddressInterfaceFactory; |
13 | 14 | use Magento\Customer\Model\AddressFactory;
|
14 | 15 | use Magento\Framework\App\ObjectManager;
|
15 | 16 | use Magento\Framework\Validator\Exception as ValidatorException;
|
@@ -46,24 +47,32 @@ class CustomerManagement
|
46 | 47 | */
|
47 | 48 | private $addressFactory;
|
48 | 49 |
|
| 50 | + /** |
| 51 | + * @var AddressInterfaceFactory |
| 52 | + */ |
| 53 | + private $customerAddressFactory; |
| 54 | + |
49 | 55 | /**
|
50 | 56 | * CustomerManagement constructor.
|
51 | 57 | * @param CustomerRepository $customerRepository
|
52 | 58 | * @param CustomerAddressRepository $customerAddressRepository
|
53 | 59 | * @param AccountManagement $accountManagement
|
| 60 | + * @param AddressInterfaceFactory $customerAddressFactory |
54 | 61 | * @param ValidatorFactory|null $validatorFactory
|
55 | 62 | * @param AddressFactory|null $addressFactory
|
56 | 63 | */
|
57 | 64 | public function __construct(
|
58 | 65 | CustomerRepository $customerRepository,
|
59 | 66 | CustomerAddressRepository $customerAddressRepository,
|
60 | 67 | AccountManagement $accountManagement,
|
| 68 | + AddressInterfaceFactory $customerAddressFactory, |
61 | 69 | ValidatorFactory $validatorFactory = null,
|
62 | 70 | AddressFactory $addressFactory = null
|
63 | 71 | ) {
|
64 | 72 | $this->customerRepository = $customerRepository;
|
65 | 73 | $this->customerAddressRepository = $customerAddressRepository;
|
66 | 74 | $this->accountManagement = $accountManagement;
|
| 75 | + $this->customerAddressFactory = $customerAddressFactory; |
67 | 76 | $this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
|
68 | 77 | ->get(ValidatorFactory::class);
|
69 | 78 | $this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
|
@@ -144,24 +153,34 @@ public function validateAddresses(QuoteEntity $quote)
|
144 | 153 | $addresses[] = $this->customerAddressRepository->getById(
|
145 | 154 | $quote->getBillingAddress()->getCustomerAddressId()
|
146 | 155 | );
|
| 156 | + } else { |
| 157 | + $billingAddress = $quote->getBillingAddress(); |
| 158 | + $customerAddress = $this->customerAddressFactory->create(); |
| 159 | + $customerAddress->setFirstname($billingAddress->getFirstname()); |
| 160 | + $customerAddress->setLastname($billingAddress->getLastname()); |
| 161 | + $customerAddress->setStreet($billingAddress->getStreet()); |
| 162 | + $customerAddress->setCity($billingAddress->getCity()); |
| 163 | + $customerAddress->setPostcode($billingAddress->getPostcode()); |
| 164 | + $customerAddress->setTelephone($billingAddress->getTelephone()); |
| 165 | + $customerAddress->setCountryId($billingAddress->getCountryId()); |
| 166 | + $addresses[] = $customerAddress; |
147 | 167 | }
|
148 | 168 | if ($quote->getShippingAddress()->getCustomerAddressId()) {
|
149 | 169 | $addresses[] = $this->customerAddressRepository->getById(
|
150 | 170 | $quote->getShippingAddress()->getCustomerAddressId()
|
151 | 171 | );
|
152 | 172 | }
|
153 |
| - if (!empty($addresses)) { |
154 |
| - foreach ($addresses as $address) { |
155 |
| - $validator = $this->validatorFactory->createValidator('customer_address', 'save'); |
156 |
| - $addressModel = $this->addressFactory->create(); |
157 |
| - $addressModel->updateData($address); |
158 |
| - if (!$validator->isValid($addressModel)) { |
159 |
| - throw new ValidatorException( |
160 |
| - null, |
161 |
| - null, |
162 |
| - $validator->getMessages() |
163 |
| - ); |
164 |
| - } |
| 173 | + |
| 174 | + foreach ($addresses as $address) { |
| 175 | + $validator = $this->validatorFactory->createValidator('customer_address', 'save'); |
| 176 | + $addressModel = $this->addressFactory->create(); |
| 177 | + $addressModel->updateData($address); |
| 178 | + if (!$validator->isValid($addressModel)) { |
| 179 | + throw new ValidatorException( |
| 180 | + null, |
| 181 | + null, |
| 182 | + $validator->getMessages() |
| 183 | + ); |
165 | 184 | }
|
166 | 185 | }
|
167 | 186 | }
|
|
0 commit comments