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 1c3dac1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ scripts/ export-ignore
.gitignore export-ignore
.gitattributes export-ignore
.prettierrc export-ignore
.pre-commit-config.yaml export-ignore
bumpver.toml export-ignore
docker-compose.yaml export-ignore
README.md export-ignore
Expand Down
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ minimum_pre_commit_version: 3.2.0
default_install_hook_types: [pre-commit, commit-msg, prepare-commit-msg]
default_stages: [pre-commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-ast
- id: check-added-large-files
- id: check-json
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
- id: pretty-format-json
args: ["--indent=\t", '--no-sort-keys']
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
Expand Down
84 changes: 61 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,62 @@ 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
$keys = [
'parent_id',
'firstname',
'middlename',
'lastname',
'street',
'city',
'postcode',
'country_id',
'region_id',
'region',
'telephone',
'company',
];
foreach ($keys as $key) {
$value = ($key === 'parent_id')
? $order->getCustomerId()
: $billingAddress->getData($key);
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 1c3dac1

Please sign in to comment.