11<?php
22/**
3- * Customer address entity resource model
4- *
53 * Copyright © Magento, Inc. All rights reserved.
64 * See COPYING.txt for license details.
75 */
6+ declare (strict_types=1 );
7+
88namespace Magento \Customer \Model \ResourceModel \Address ;
99
10+ use Magento \Customer \Api \Data \CustomerInterface ;
1011use Magento \Customer \Model \Address ;
1112use Magento \Customer \Model \Customer ;
13+ use Magento \Customer \Model \CustomerFactory ;
14+ use Magento \Customer \Model \CustomerRegistry ;
15+ use Magento \Framework \Model \AbstractModel ;
1216use Magento \Framework \Model \ResourceModel \Db \VersionControl \RelationInterface ;
1317
1418/**
1721class Relation implements RelationInterface
1822{
1923 /**
20- * @var \Magento\Customer\Model\ CustomerFactory
24+ * @var CustomerFactory
2125 */
2226 protected $ customerFactory ;
2327
2428 /**
25- * @param \Magento\Customer\Model\CustomerFactory $customerFactory
29+ * @var CustomerRegistry
2630 */
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+ ) {
2941 $ this ->customerFactory = $ customerFactory ;
42+ $ this ->customerRegistry = $ customerRegistry ;
3043 }
3144
3245 /**
3346 * Process object relations
3447 *
35- * @param \Magento\Framework\Model\ AbstractModel $object
48+ * @param AbstractModel $object
3649 * @return void
3750 */
38- public function processRelation (\ Magento \ Framework \ Model \ AbstractModel $ object )
51+ public function processRelation (AbstractModel $ object ): void
3952 {
40- /**
41- * @var $object Address
42- */
53+ /** @var $object Address */
4354 if (!$ object ->getIsCustomerSaveTransaction () && $ object ->getId ()) {
4455 $ customer = $ this ->customerFactory ->create ()->load ($ object ->getCustomerId ());
4556
@@ -53,6 +64,7 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
5364 $ changedAddresses ,
5465 $ customer ->getResource ()->getConnection ()->quoteInto ('entity_id = ? ' , $ customer ->getId ())
5566 );
67+ $ this ->updateCustomerRegistry ($ customer , $ changedAddresses );
5668 }
5769 }
5870 }
@@ -71,12 +83,12 @@ private function getDefaultBillingChangedAddress(
7183 array $ changedAddresses
7284 ): array {
7385 if ($ object ->getIsDefaultBilling ()) {
74- $ changedAddresses [' default_billing ' ] = $ object ->getId ();
86+ $ changedAddresses [CustomerInterface:: DEFAULT_BILLING ] = $ object ->getId ();
7587 } elseif ($ customer ->getDefaultBillingAddress ()
7688 && $ object ->getIsDefaultBilling () === false
7789 && (int )$ customer ->getDefaultBillingAddress ()->getId () === (int )$ object ->getId ()
7890 ) {
79- $ changedAddresses [' default_billing ' ] = null ;
91+ $ changedAddresses [CustomerInterface:: DEFAULT_BILLING ] = null ;
8092 }
8193
8294 return $ changedAddresses ;
@@ -96,27 +108,47 @@ private function getDefaultShippingChangedAddress(
96108 array $ changedAddresses
97109 ): array {
98110 if ($ object ->getIsDefaultShipping ()) {
99- $ changedAddresses [' default_shipping ' ] = $ object ->getId ();
111+ $ changedAddresses [CustomerInterface:: DEFAULT_SHIPPING ] = $ object ->getId ();
100112 } elseif ($ customer ->getDefaultShippingAddress ()
101113 && $ object ->getIsDefaultShipping () === false
102114 && (int )$ customer ->getDefaultShippingAddress ()->getId () === (int )$ object ->getId ()
103115 ) {
104- $ changedAddresses [' default_shipping ' ] = null ;
116+ $ changedAddresses [CustomerInterface:: DEFAULT_SHIPPING ] = null ;
105117 }
106118
107119 return $ changedAddresses ;
108120 }
109121
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+
110142 /**
111143 * Checks if address has chosen as default and has had an id
112144 *
113145 * @deprecated Is not used anymore due to changes in logic of save of address.
114146 * If address was default and becomes not default than default address id for customer must be
115147 * set to null
116- * @param \Magento\Framework\Model\ AbstractModel $object
148+ * @param AbstractModel $object
117149 * @return bool
118150 */
119- protected function isAddressDefault (\ Magento \ Framework \ Model \ AbstractModel $ object )
151+ protected function isAddressDefault (AbstractModel $ object )
120152 {
121153 return $ object ->getId () && ($ object ->getIsDefaultBilling () || $ object ->getIsDefaultShipping ());
122154 }
0 commit comments