Skip to content

Commit 9e84c71

Browse files
authored
Merge pull request #26 from tatiana-scandi/bugfix/3671/fix-region-saving
#3671 - fix saving region after changing address
2 parents 5371a4f + 57c0f1d commit 9e84c71

2 files changed

Lines changed: 129 additions & 1 deletion

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* ScandiPWA - Progressive Web App for Magento
4+
*
5+
* Copyright © Scandiweb, Inc. All rights reserved.
6+
* See LICENSE for license details.
7+
*
8+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
9+
* @package scandipwa/module-customer-graph-ql
10+
* @link https://github.com/scandipwa/module-customer-graph-ql
11+
*/
12+
declare(strict_types=1);
13+
14+
namespace ScandiPWA\CustomerGraphQl\Model\Customer\Address;
15+
16+
use Magento\Customer\Api\AddressRepositoryInterface;
17+
use Magento\Customer\Api\Data\AddressInterface;
18+
use Magento\CustomerGraphQl\Model\Customer\Address\GetAllowedAddressAttributes;
19+
use Magento\CustomerGraphQl\Model\Customer\Address\PopulateCustomerAddressFromInput;
20+
use Magento\CustomerGraphQl\Model\Customer\Address\UpdateCustomerAddress as SourceAddress;
21+
use Magento\Directory\Helper\Data as DirectoryData;
22+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
23+
use Magento\Framework\Api\DataObjectHelper;
24+
use Magento\Framework\Exception\LocalizedException;
25+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
26+
27+
/**
28+
* @inheridoc
29+
*/
30+
class UpdateCustomerAddress extends SourceAddress
31+
{
32+
/**
33+
* @var AddressRepositoryInterface
34+
*/
35+
protected $addressRepository;
36+
37+
/**
38+
* @var DataObjectHelper
39+
*/
40+
protected $dataObjectHelper;
41+
42+
/**
43+
* @var array
44+
*/
45+
protected $restrictedKeys;
46+
47+
/**
48+
* @var ValidateAddress
49+
*/
50+
protected $addressValidator;
51+
52+
/**
53+
* @var PopulateCustomerAddressFromInput
54+
*/
55+
protected $populateCustomerAddressFromInput;
56+
57+
/**
58+
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
59+
* @param AddressRepositoryInterface $addressRepository
60+
* @param DataObjectHelper $dataObjectHelper
61+
* @param DirectoryData $directoryData
62+
* @param RegionCollectionFactory $regionCollectionFactory
63+
* @param ValidateAddress $addressValidator
64+
* @param PopulateCustomerAddressFromInput $populateCustomerAddressFromInput
65+
* @param array $restrictedKeys
66+
*/
67+
public function __construct(
68+
GetAllowedAddressAttributes $getAllowedAddressAttributes,
69+
AddressRepositoryInterface $addressRepository,
70+
DataObjectHelper $dataObjectHelper,
71+
DirectoryData $directoryData,
72+
RegionCollectionFactory $regionCollectionFactory,
73+
ValidateAddress $addressValidator,
74+
PopulateCustomerAddressFromInput $populateCustomerAddressFromInput,
75+
array $restrictedKeys = []
76+
) {
77+
parent::__construct(
78+
$getAllowedAddressAttributes,
79+
$addressRepository,
80+
$dataObjectHelper,
81+
$directoryData,
82+
$regionCollectionFactory,
83+
$addressValidator,
84+
$populateCustomerAddressFromInput,
85+
$restrictedKeys
86+
);
87+
88+
$this->addressRepository = $addressRepository;
89+
$this->dataObjectHelper = $dataObjectHelper;
90+
$this->addressValidator = $addressValidator;
91+
$this->populateCustomerAddressFromInput = $populateCustomerAddressFromInput;
92+
$this->restrictedKeys = $restrictedKeys;
93+
}
94+
95+
/**
96+
* @inheridoc
97+
*/
98+
public function execute(AddressInterface $address, array $data): void
99+
{
100+
if (isset($data['country_code'])) {
101+
$data['country_id'] = $data['country_code'];
102+
}
103+
$this->validateData($data);
104+
105+
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
106+
$this->dataObjectHelper->populateWithArray($address, $filteredData, AddressInterface::class);
107+
108+
if (!empty($data['region']['region_id'])) {
109+
$address->setRegionId($address->getRegion()->getRegionId());
110+
} else {
111+
$data['region']['region_id'] = null;
112+
113+
# If new address doesn't have selectable region, make sure that old region id is removed from DB
114+
$address->setRegionId(null);
115+
}
116+
117+
$this->populateCustomerAddressFromInput->execute($address, $filteredData);
118+
$this->addressValidator->execute($address);
119+
120+
try {
121+
$this->addressRepository->save($address);
122+
} catch (LocalizedException $e) {
123+
throw new GraphQlInputException(__($e->getMessage()), $e);
124+
}
125+
}
126+
}

src/etc/di.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
type="ScandiPWA\CustomerGraphQl\Model\Customer\GetCustomer"/>
1717
<preference for="Magento\CustomerGraphQl\Model\Customer\Address\ValidateAddress"
1818
type="ScandiPWA\CustomerGraphQl\Model\Customer\Address\ValidateAddress"/>
19+
<preference for="Magento\CustomerGraphQl\Model\Customer\Address\UpdateCustomerAddress"
20+
type="ScandiPWA\CustomerGraphQl\Model\Customer\Address\UpdateCustomerAddress"/>
1921
<preference for="Magento\CustomerGraphQl\Model\Customer\CheckCustomerPassword"
2022
type="ScandiPWA\CustomerGraphQl\Model\Customer\CheckCustomerPassword"/>
2123
<preference for="Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext"
@@ -25,4 +27,4 @@
2527
<argument name="customerSession" xsi:type="object">ScandiPWA\CustomerGraphQl\Model\Session</argument>
2628
</arguments>
2729
</type>
28-
</config>
30+
</config>

0 commit comments

Comments
 (0)