Skip to content

Commit b29e327

Browse files
authored
Merge pull request #60 from two-inc/brtkwr-two/cet-537-fix-issue-with-saving-address-for-logged-in-customers
CET-537/fix: Issue with saving address for logged in customers
2 parents 13c702d + 98bb264 commit b29e327

File tree

7 files changed

+44
-77
lines changed

7 files changed

+44
-77
lines changed

Controller/Payment/Cancel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct(
5252
*/
5353
public function execute()
5454
{
55+
$order = null;
5556
try {
5657
$order = $this->orderService->getOrderByReference();
5758
$this->orderService->cancelTwoOrder($order);
@@ -62,10 +63,9 @@ public function execute()
6263
throw new LocalizedException($message);
6364
} catch (Exception $exception) {
6465
$this->orderService->restoreQuote();
65-
if (isset($order)) {
66+
if ($order !== null) {
6667
$this->orderService->failOrder($order, $exception->getMessage());
6768
}
68-
6969
$this->messageManager->addErrorMessage($exception->getMessage());
7070
return $this->getResponse()->setRedirect($this->_url->getUrl('checkout/cart'));
7171
}

Controller/Payment/Confirm.php

Lines changed: 36 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Exception;
1111
use Magento\Customer\Api\AddressRepositoryInterface;
12-
use Magento\Framework\Api\SearchCriteriaInterface;
1312
use Magento\Framework\App\Action\Action;
1413
use Magento\Framework\App\Action\Context;
1514
use Magento\Framework\App\ResponseInterface;
@@ -33,11 +32,6 @@ class Confirm extends Action
3332
*/
3433
private $addressRepository;
3534

36-
/**
37-
* @var SearchCriteriaInterface
38-
*/
39-
private $searchCriteria;
40-
4135
/**
4236
* @var OrderService
4337
*/
@@ -51,13 +45,11 @@ class Confirm extends Action
5145
public function __construct(
5246
Context $context,
5347
AddressRepositoryInterface $addressRepository,
54-
SearchCriteriaInterface $searchCriteria,
5548
OrderService $orderService,
5649
OrderSender $orderSender,
5750
ConfigRepository $configRepository
5851
) {
5952
$this->addressRepository = $addressRepository;
60-
$this->searchCriteria = $searchCriteria;
6153
$this->orderService = $orderService;
6254
$this->orderSender = $orderSender;
6355
$this->configRepository = $configRepository;
@@ -70,6 +62,7 @@ public function __construct(
7062
*/
7163
public function execute()
7264
{
65+
$order = null;
7366
try {
7467
$order = $this->orderService->getOrderByReference();
7568
$twoOrder = $this->orderService->getTwoOrderFromApi($order);
@@ -79,9 +72,21 @@ public function execute()
7972
}
8073
$this->orderSender->send($order);
8174
try {
82-
$this->saveAddress($order, $twoOrder);
75+
$this->updateCustomerAddress($order, $twoOrder);
76+
} catch (LocalizedException $exception) {
77+
$message = __(
78+
"Failed to update %1 customer address: %2",
79+
$this->configRepository::PROVIDER,
80+
$exception->getMessage()
81+
);
82+
$this->orderService->addOrderComment($order, $message);
8383
} catch (Exception $exception) {
84-
$this->orderService->addOrderComment($order, $exception->getMessage());
84+
$message = __(
85+
"Failed to update %1 customer address: %2",
86+
$this->configRepository::PROVIDER,
87+
$exception->getMessage()
88+
);
89+
$this->orderService->addOrderComment($order, $message);
8590
}
8691
$this->orderService->processOrder($order, $twoOrder['id']);
8792
return $this->getResponse()->setRedirect($this->_url->getUrl('checkout/onepage/success'));
@@ -99,87 +104,45 @@ public function execute()
99104
}
100105
} catch (Exception $exception) {
101106
$this->orderService->restoreQuote();
102-
if (isset($order)) {
107+
if ($order !== null) {
103108
$this->orderService->failOrder($order, $exception->getMessage());
104109
}
105-
106110
$this->messageManager->addErrorMessage($exception->getMessage());
107111
return $this->getResponse()->setRedirect($this->_url->getUrl('checkout/cart'));
108112
}
109113
}
110114

111115
/**
112-
* Save address
116+
* Update customer address
113117
*
114118
* @param $order
115119
* @param array $twoOrder
116120
*
121+
* @return void
117122
* @throws Exception
118123
*/
119-
private function saveAddress($order, $twoOrder)
124+
private function updateCustomerAddress($order, $twoOrder)
120125
{
121-
if ($order->getCustomerId()) {
122-
if ($order->getBillingAddress()->getCustomerAddressId()) {
123-
$customerAddress = $this->addressRepository->getById(
124-
$order->getBillingAddress()->getCustomerAddressId()
125-
);
126-
} else {
127-
$this->searchCriteria
128-
->setField('parent_id')
129-
->setValue($order->getCustomerId())
130-
->setConditionType('eq');
131-
$customerAddressCollection = $this->addressRepository
132-
->getList($this->searchCriteria)
133-
->getItems();
134-
$customerAddress = $customerAddressCollection[0] ?? null;
135-
}
126+
$customerAddress = null;
127+
if ($order->getBillingAddress()->getCustomerAddressId()) {
128+
$customerAddress = $this->addressRepository->getById(
129+
$order->getBillingAddress()->getCustomerAddressId()
130+
);
136131
if ($customerAddress && $customerAddress->getId()) {
137-
$this->copyMetadataToAddress(
138-
$twoOrder,
139-
$customerAddress
140-
);
132+
if (isset($twoOrder['buyer']['company']['organization_number'])) {
133+
$customerAddress->setData('company_id', $twoOrder['buyer']['company']['organization_number']);
134+
}
135+
if (isset($twoOrder['buyer']['company']['company_name'])) {
136+
$customerAddress->setData('company_name', $twoOrder['buyer']['company']['company_name']);
137+
}
138+
if (isset($twoOrder['buyer_department'])) {
139+
$customerAddress->setData('department', $twoOrder['buyer_department']);
140+
}
141+
if (isset($twoOrder['buyer_project'])) {
142+
$customerAddress->setData('project', $twoOrder['buyer_project']);
143+
}
141144
$this->addressRepository->save($customerAddress);
142145
}
143-
} else {
144-
// Save metadata to shipping address
145-
$shippingAddress = $order->getShippingAddress();
146-
$this->copyMetadataToAddress(
147-
$twoOrder,
148-
$shippingAddress
149-
);
150-
$shippingAddress->save();
151-
// Save metadata to billing address
152-
$billingAddress = $order->getBillingAddress();
153-
$this->copyMetadataToAddress(
154-
$twoOrder,
155-
$billingAddress
156-
);
157-
$billingAddress->save();
158-
}
159-
}
160-
161-
/**
162-
* Set metadata to customer address
163-
*
164-
* @param array $twoOrder
165-
* @param $address
166-
*
167-
* @return void
168-
* @throws Exception
169-
*/
170-
private function copyMetadataToAddress(array $twoOrder, $address)
171-
{
172-
if (isset($twoOrder['buyer']['company']['organization_number'])) {
173-
$address->setData('company_id', $twoOrder['buyer']['company']['organization_number']);
174-
}
175-
if (isset($twoOrder['buyer']['company']['company_name'])) {
176-
$address->setData('company_name', $twoOrder['buyer']['company']['company_name']);
177-
}
178-
if (isset($twoOrder['buyer_department'])) {
179-
$address->setData('department', $twoOrder['buyer_department']);
180-
}
181-
if (isset($twoOrder['buyer_project'])) {
182-
$address->setData('project', $twoOrder['buyer_project']);
183146
}
184147
}
185148
}

Controller/Payment/Verificationfailed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(
5050
*/
5151
public function execute()
5252
{
53+
$order = null;
5354
try {
5455
$order = $this->orderService->getOrderByReference();
5556
$message = __(
@@ -59,10 +60,9 @@ public function execute()
5960
throw new LocalizedException($message);
6061
} catch (Exception $exception) {
6162
$this->orderService->restoreQuote();
62-
if (isset($order)) {
63+
if ($order !== null) {
6364
$this->orderService->failOrder($order, $exception->getMessage());
6465
}
65-
6666
$this->messageManager->addErrorMessage($exception->getMessage());
6767
return $this->getResponse()->setRedirect($this->_url->getUrl('checkout/cart'));
6868
}

i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Download,Download
4848
"Enter details manually","Enter details manually"
4949
Environment,Environment
5050
"Failed to refund order with %1. Reason: %2","Failed to refund order with %1. Reason: %2"
51+
"Failed to update %1 customer address: %2","Failed to update %1 customer address: %2"
5152
"Find out more","Find out more"
5253
"First Name is not valid.","First Name is not valid."
5354
"Fulfilment order status","Fulfilment order status"

i18n/nb_NO.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Download,"Last ned"
4848
"Enter details manually","Skriv inn detaljer manuelt"
4949
Environment,Miljø
5050
"Failed to refund order with %1. Reason: %2","Kunne ikke refundere bestillingen med %1. Årsak: %2"
51+
"Failed to update %1 customer address: %2","Kunne ikke oppdatere %1 kundeadresse: %2"
5152
"Find out more","Finn ut mer"
5253
"First Name is not valid.","Fornavn er ikke gyldig."
5354
"Fulfilment order status","Oppfyllelse av ordrestatus"

i18n/nl_NL.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Download,Download
4848
"Enter details manually","Gegevens handmatig invoeren"
4949
Environment,Omgeving
5050
"Failed to refund order with %1. Reason: %2","Het is niet gelukt om de bestelling met %1 terug te betalen. Reden: %2"
51+
"Failed to update %1 customer address: %2","Kon het adres van klant %1 niet bijwerken: %2"
5152
"Find out more","Meer informatie"
5253
"First Name is not valid.","Voornaam is niet geldig."
5354
"Fulfilment order status","Status van vervulling bestelling"

i18n/sv_SE.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Download,"Ladda ner"
4848
"Enter details manually","Ange detaljer manuellt"
4949
Environment,Miljö
5050
"Failed to refund order with %1. Reason: %2","Det gick inte att återbetala beställningen med %1. Orsak: %2"
51+
"Failed to update %1 customer address: %2","Misslyckades med att uppdatera %1 kundadress: %2"
5152
"Find out more","Ta reda på mer"
5253
"First Name is not valid.","Förnamn är inte giltigt."
5354
"Fulfilment order status","Uppfyllande orderstatus"

0 commit comments

Comments
 (0)