Skip to content

Commit

Permalink
CET-537/fix: Ensure address can be updated using search criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkwr committed Dec 6, 2024
1 parent aa53a22 commit 02b1771
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions Controller/Payment/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
Expand All @@ -32,6 +33,11 @@ class Confirm extends Action
*/
private $addressRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var OrderService
*/
Expand All @@ -45,11 +51,13 @@ class Confirm extends Action
public function __construct(
Context $context,
AddressRepositoryInterface $addressRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderService $orderService,
OrderSender $orderSender,
ConfigRepository $configRepository
) {
$this->addressRepository = $addressRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderService = $orderService;
$this->orderSender = $orderSender;
$this->configRepository = $configRepository;
Expand All @@ -69,17 +77,10 @@ public function execute()
if (isset($twoOrder['state']) && $twoOrder['state'] != 'UNVERIFIED') {
if (in_array($twoOrder['state'], ['VERIFIED', 'CONFIRMED'])) {
$this->orderService->confirmOrder($order);
$this->orderSender->send($order);
}
$this->orderSender->send($order);
try {
$this->updateCustomerAddress($order, $twoOrder);
} catch (LocalizedException $exception) {
$message = __(
"Failed to update %1 customer address: %2",
$this->configRepository::PROVIDER,
$exception->getMessage()
);
$this->orderService->addOrderComment($order, $message);
} catch (Exception $exception) {
$message = __(
"Failed to update %1 customer address: %2",
Expand Down Expand Up @@ -124,25 +125,59 @@ public function execute()
private function updateCustomerAddress($order, $twoOrder)
{
$customerAddress = null;
if ($order->getBillingAddress()->getCustomerAddressId()) {
$billingAddress = $order->getBillingAddress();
if ($billingAddress->getCustomerAddressId()) {
// Try to load the customer address by ID
$customerAddress = $this->addressRepository->getById(
$order->getBillingAddress()->getCustomerAddressId()
$billingAddress->getCustomerAddressId()
);
if ($customerAddress && $customerAddress->getId()) {
if (isset($twoOrder['buyer']['company']['organization_number'])) {
$customerAddress->setData('company_id', $twoOrder['buyer']['company']['organization_number']);
}
if (isset($twoOrder['buyer']['company']['company_name'])) {
$customerAddress->setData('company_name', $twoOrder['buyer']['company']['company_name']);
}
if (isset($twoOrder['buyer_department'])) {
$customerAddress->setData('department', $twoOrder['buyer_department']);
}
if (isset($twoOrder['buyer_project'])) {
$customerAddress->setData('project', $twoOrder['buyer_project']);
}
if ($customerAddress == null && $order->getCustomerId()) {
// Build a search criteria to find customer addresse that matches the billing address
$filters = [
'parent_id' => $order->getCustomerId(),
'firstname' => $billingAddress->getData('firstname'),
'middlename' => $billingAddress->getData('middlename'),
'lastname' => $billingAddress->getData('lastname'),
'street' => $billingAddress->getData('street'),
'city' => $billingAddress->getData('city'),
'postcode' => $billingAddress->getData('postcode'),
'country_id' => $billingAddress->getData('country_id'),
'region_id' => $billingAddress->getData('region_id'),
'region' => $billingAddress->getData('region'),
'telephone' => $billingAddress->getData('telephone'),
'company' => $billingAddress->getData('company'),
];
foreach ($filters as $key => $value) {
if (!empty($value)) {
$this->searchCriteriaBuilder->addFilter($key, $value, 'eq');
}
$this->addressRepository->save($customerAddress);
}
$searchCriteria = $this->searchCriteriaBuilder->create();
$customerAddressCollection = $this->addressRepository
->getList($searchCriteria)
->getItems();
$customerAddress = $customerAddressCollection[0] ?? null;
}
if ($customerAddress && $customerAddress->getId()) {
if (isset($twoOrder['buyer']['company']['organization_number'])) {
$customerAddress->setData('company_id', $twoOrder['buyer']['company']['organization_number']);
}
if (isset($twoOrder['buyer']['company']['company_name'])) {
$customerAddress->setData('company_name', $twoOrder['buyer']['company']['company_name']);
}
if (isset($twoOrder['buyer_department'])) {
$customerAddress->setData('department', $twoOrder['buyer_department']);
}
if (isset($twoOrder['buyer_project'])) {
$customerAddress->setData('project', $twoOrder['buyer_project']);
}
$this->addressRepository->save($customerAddress);
$message = __(
"%1 customer address updated.",
$this->configRepository::PROVIDER,
);
$this->orderService->addOrderComment($order, $message);
}
}
}

0 comments on commit 02b1771

Please sign in to comment.