1
1
<?php
2
2
/**
3
- * Customer address entity resource model
4
- *
5
3
* Copyright © Magento, Inc. All rights reserved.
6
4
* See COPYING.txt for license details.
7
5
*/
6
+ declare (strict_types=1 );
7
+
8
8
namespace Magento \Customer \Model \ResourceModel \Address ;
9
9
10
+ use Magento \Customer \Api \Data \CustomerInterface ;
10
11
use Magento \Customer \Model \Address ;
11
12
use Magento \Customer \Model \Customer ;
13
+ use Magento \Customer \Model \CustomerFactory ;
14
+ use Magento \Customer \Model \CustomerRegistry ;
15
+ use Magento \Framework \Model \AbstractModel ;
12
16
use Magento \Framework \Model \ResourceModel \Db \VersionControl \RelationInterface ;
13
17
14
18
/**
17
21
class Relation implements RelationInterface
18
22
{
19
23
/**
20
- * @var \Magento\Customer\Model\ CustomerFactory
24
+ * @var CustomerFactory
21
25
*/
22
26
protected $ customerFactory ;
23
27
24
28
/**
25
- * @param \Magento\Customer\Model\CustomerFactory $customerFactory
29
+ * @var CustomerRegistry
26
30
*/
27
- public function __construct (\Magento \Customer \Model \CustomerFactory $ customerFactory )
28
- {
31
+ private $ customerRegistry ;
32
+
33
+ /**
34
+ * @param CustomerFactory $customerFactory
35
+ * @param CustomerRegistry $customerRegistry
36
+ */
37
+ public function __construct (
38
+ CustomerFactory $ customerFactory ,
39
+ CustomerRegistry $ customerRegistry
40
+ ) {
29
41
$ this ->customerFactory = $ customerFactory ;
42
+ $ this ->customerRegistry = $ customerRegistry ;
30
43
}
31
44
32
45
/**
33
46
* Process object relations
34
47
*
35
- * @param \Magento\Framework\Model\ AbstractModel $object
48
+ * @param AbstractModel $object
36
49
* @return void
37
50
*/
38
- public function processRelation (\ Magento \ Framework \ Model \ AbstractModel $ object )
51
+ public function processRelation (AbstractModel $ object ): void
39
52
{
40
- /**
41
- * @var $object Address
42
- */
53
+ /** @var $object Address */
43
54
if (!$ object ->getIsCustomerSaveTransaction () && $ object ->getId ()) {
44
55
$ customer = $ this ->customerFactory ->create ()->load ($ object ->getCustomerId ());
45
56
@@ -53,6 +64,7 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
53
64
$ changedAddresses ,
54
65
$ customer ->getResource ()->getConnection ()->quoteInto ('entity_id = ? ' , $ customer ->getId ())
55
66
);
67
+ $ this ->updateCustomerRegistry ($ customer , $ changedAddresses );
56
68
}
57
69
}
58
70
}
@@ -71,12 +83,12 @@ private function getDefaultBillingChangedAddress(
71
83
array $ changedAddresses
72
84
): array {
73
85
if ($ object ->getIsDefaultBilling ()) {
74
- $ changedAddresses [' default_billing ' ] = $ object ->getId ();
86
+ $ changedAddresses [CustomerInterface:: DEFAULT_BILLING ] = $ object ->getId ();
75
87
} elseif ($ customer ->getDefaultBillingAddress ()
76
88
&& $ object ->getIsDefaultBilling () === false
77
89
&& (int )$ customer ->getDefaultBillingAddress ()->getId () === (int )$ object ->getId ()
78
90
) {
79
- $ changedAddresses [' default_billing ' ] = null ;
91
+ $ changedAddresses [CustomerInterface:: DEFAULT_BILLING ] = null ;
80
92
}
81
93
82
94
return $ changedAddresses ;
@@ -96,27 +108,47 @@ private function getDefaultShippingChangedAddress(
96
108
array $ changedAddresses
97
109
): array {
98
110
if ($ object ->getIsDefaultShipping ()) {
99
- $ changedAddresses [' default_shipping ' ] = $ object ->getId ();
111
+ $ changedAddresses [CustomerInterface:: DEFAULT_SHIPPING ] = $ object ->getId ();
100
112
} elseif ($ customer ->getDefaultShippingAddress ()
101
113
&& $ object ->getIsDefaultShipping () === false
102
114
&& (int )$ customer ->getDefaultShippingAddress ()->getId () === (int )$ object ->getId ()
103
115
) {
104
- $ changedAddresses [' default_shipping ' ] = null ;
116
+ $ changedAddresses [CustomerInterface:: DEFAULT_SHIPPING ] = null ;
105
117
}
106
118
107
119
return $ changedAddresses ;
108
120
}
109
121
122
+ /**
123
+ * Push updated customer entity to the registry.
124
+ *
125
+ * @param Customer $customer
126
+ * @param array $changedAddresses
127
+ * @return void
128
+ */
129
+ private function updateCustomerRegistry (Customer $ customer , array $ changedAddresses ): void
130
+ {
131
+ if (array_key_exists (CustomerInterface::DEFAULT_BILLING , $ changedAddresses )) {
132
+ $ customer ->setDefaultBilling ($ changedAddresses [CustomerInterface::DEFAULT_BILLING ]);
133
+ }
134
+
135
+ if (array_key_exists (CustomerInterface::DEFAULT_SHIPPING , $ changedAddresses )) {
136
+ $ customer ->setDefaultShipping ($ changedAddresses [CustomerInterface::DEFAULT_SHIPPING ]);
137
+ }
138
+
139
+ $ this ->customerRegistry ->push ($ customer );
140
+ }
141
+
110
142
/**
111
143
* Checks if address has chosen as default and has had an id
112
144
*
113
145
* @deprecated Is not used anymore due to changes in logic of save of address.
114
146
* If address was default and becomes not default than default address id for customer must be
115
147
* set to null
116
- * @param \Magento\Framework\Model\ AbstractModel $object
148
+ * @param AbstractModel $object
117
149
* @return bool
118
150
*/
119
- protected function isAddressDefault (\ Magento \ Framework \ Model \ AbstractModel $ object )
151
+ protected function isAddressDefault (AbstractModel $ object )
120
152
{
121
153
return $ object ->getId () && ($ object ->getIsDefaultBilling () || $ object ->getIsDefaultShipping ());
122
154
}
0 commit comments