Skip to content

Commit 354ddb7

Browse files
committed
AC-14987::company field validation fails for guest checkout
1 parent e3f780b commit 354ddb7

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

app/code/Magento/Quote/Model/CustomerManagement.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Magento\Framework\Validator\Exception as ValidatorException;
1717
use Magento\Framework\Validator\Factory as ValidatorFactory;
1818
use Magento\Quote\Model\Quote as QuoteEntity;
19+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
20+
use Magento\Customer\Api\Data\AddressInterface;
21+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1922

2023
/**
2124
* Class Customer
@@ -52,12 +55,18 @@ class CustomerManagement
5255
*/
5356
private $customerAddressFactory;
5457

58+
/**
59+
* @var RegionInterfaceFactory
60+
*/
61+
private $regionFactory;
62+
5563
/**
5664
* CustomerManagement constructor.
5765
* @param CustomerRepository $customerRepository
5866
* @param CustomerAddressRepository $customerAddressRepository
5967
* @param AccountManagement $accountManagement
6068
* @param AddressInterfaceFactory $customerAddressFactory
69+
* @param RegionInterfaceFactory $regionFactory
6170
* @param ValidatorFactory|null $validatorFactory
6271
* @param AddressFactory|null $addressFactory
6372
*/
@@ -66,13 +75,15 @@ public function __construct(
6675
CustomerAddressRepository $customerAddressRepository,
6776
AccountManagement $accountManagement,
6877
AddressInterfaceFactory $customerAddressFactory,
78+
RegionInterfaceFactory $regionFactory,
6979
?ValidatorFactory $validatorFactory = null,
7080
?AddressFactory $addressFactory = null
7181
) {
7282
$this->customerRepository = $customerRepository;
7383
$this->customerAddressRepository = $customerAddressRepository;
7484
$this->accountManagement = $accountManagement;
7585
$this->customerAddressFactory = $customerAddressFactory;
86+
$this->regionFactory = $regionFactory;
7687
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
7788
->get(ValidatorFactory::class);
7889
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
@@ -161,17 +172,7 @@ public function validateAddresses(QuoteEntity $quote)
161172
}
162173
if (empty($addresses) && $quote->getCustomerIsGuest()) {
163174
$billingAddress = $quote->getBillingAddress();
164-
$customerAddress = $this->customerAddressFactory->create();
165-
$customerAddress->setFirstname($billingAddress->getFirstname());
166-
$customerAddress->setMiddlename($billingAddress?->getMiddlename());
167-
$customerAddress->setLastname($billingAddress->getLastname());
168-
$customerAddress->setStreet($billingAddress->getStreet());
169-
$customerAddress->setCity($billingAddress->getCity());
170-
$customerAddress->setPostcode($billingAddress->getPostcode());
171-
$customerAddress->setTelephone($billingAddress->getTelephone());
172-
$customerAddress->setCountryId($billingAddress->getCountryId());
173-
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
174-
$addresses[] = $customerAddress;
175+
$addresses[] = $this->createCustomerAddressFromBilling($billingAddress);
175176
}
176177
foreach ($addresses as $address) {
177178
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
@@ -186,4 +187,43 @@ public function validateAddresses(QuoteEntity $quote)
186187
}
187188
}
188189
}
190+
191+
/**
192+
* Creates guest customer address from a billing address.
193+
*
194+
* @param QuoteAddress $billingAddress
195+
* @return AddressInterface
196+
*/
197+
private function createCustomerAddressFromBilling(QuoteAddress $billingAddress): AddressInterface
198+
{
199+
$customerAddress = $this->customerAddressFactory->create();
200+
$customerAddress->setPrefix($billingAddress?->getPrefix());
201+
$customerAddress->setFirstname($billingAddress->getFirstname());
202+
$customerAddress->setMiddlename($billingAddress?->getMiddlename());
203+
$customerAddress->setLastname($billingAddress->getLastname());
204+
$customerAddress->setSuffix($billingAddress?->getSuffix());
205+
$customerAddress->setCompany($billingAddress?->getCompany());
206+
$customerAddress->setStreet($billingAddress->getStreet());
207+
$customerAddress->setCountryId($billingAddress->getCountryId());
208+
$customerAddress->setCity($billingAddress->getCity());
209+
$customerAddress->setPostcode($billingAddress->getPostcode());
210+
$customerAddress->setTelephone($billingAddress->getTelephone());
211+
$customerAddress->setFax($billingAddress?->getFax());
212+
$customerAddress->setVatId($billingAddress?->getVatId());
213+
$regionData = $billingAddress->getRegion();
214+
if (is_array($regionData)) {
215+
$region = $this->regionFactory->create();
216+
$region->setRegion($regionData['region'] ?? null);
217+
$region->setRegionCode($regionData['region_code'] ?? null);
218+
$region->setRegionId($regionData['region_id'] ?? null);
219+
} elseif (is_string($regionData)) {
220+
$region = $this->regionFactory->create();
221+
$region->setRegion($regionData);
222+
} else {
223+
$region = null;
224+
}
225+
$customerAddress->setRegion($region);
226+
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
227+
return $customerAddress;
228+
}
189229
}

0 commit comments

Comments
 (0)