8
8
9
9
use Magento \Eav \Model \Config ;
10
10
use Magento \Framework \App \State ;
11
- use Magento \Quote \Model \QuoteFactory ;
12
- use Magento \Sales \Model \OrderFactory ;
13
- use Magento \Sales \Model \ResourceModel \Order \Address \CollectionFactory as AddressCollectionFactory ;
14
- use Magento \Framework \App \ResourceConnection ;
15
- use Magento \Sales \Setup \SalesSetupFactory ;
11
+ use Magento \Framework \Setup \ModuleDataSetupInterface ;
16
12
use Magento \Framework \Setup \Patch \DataPatchInterface ;
17
13
use Magento \Framework \Setup \Patch \PatchVersionInterface ;
18
- use Magento \Framework \Setup \ModuleDataSetupInterface ;
14
+ use Magento \Sales \Model \Order \Address ;
15
+ use Magento \Sales \Setup \SalesSetupFactory ;
19
16
17
+ /**
18
+ * Fills quote_address_id in table sales_order_address if it is empty.
19
+ */
20
20
class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, PatchVersionInterface
21
21
{
22
22
/**
23
23
* @var ModuleDataSetupInterface
24
24
*/
25
25
private $ moduleDataSetup ;
26
26
27
- /**
28
- * @var SalesSetupFactory
29
- */
30
- private $ salesSetupFactory ;
31
-
32
27
/**
33
28
* @var State
34
29
*/
@@ -40,44 +35,22 @@ class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, Patch
40
35
private $ eavConfig ;
41
36
42
37
/**
43
- * @var AddressCollectionFactory
44
- */
45
- private $ addressCollectionFactory ;
46
-
47
- /**
48
- * @var OrderFactory
49
- */
50
- private $ orderFactory ;
51
-
52
- /**
53
- * @var QuoteFactory
54
- */
55
- private $ quoteFactory ;
56
-
57
- /**
58
- * PatchInitial constructor.
59
38
* @param ModuleDataSetupInterface $moduleDataSetup
39
+ * @param State $state
40
+ * @param Config $eavConfig
60
41
*/
61
42
public function __construct (
62
43
ModuleDataSetupInterface $ moduleDataSetup ,
63
- SalesSetupFactory $ salesSetupFactory ,
64
44
State $ state ,
65
- Config $ eavConfig ,
66
- AddressCollectionFactory $ addressCollectionFactory ,
67
- OrderFactory $ orderFactory ,
68
- QuoteFactory $ quoteFactory
45
+ Config $ eavConfig
69
46
) {
70
47
$ this ->moduleDataSetup = $ moduleDataSetup ;
71
- $ this ->salesSetupFactory = $ salesSetupFactory ;
72
48
$ this ->state = $ state ;
73
49
$ this ->eavConfig = $ eavConfig ;
74
- $ this ->addressCollectionFactory = $ addressCollectionFactory ;
75
- $ this ->orderFactory = $ orderFactory ;
76
- $ this ->quoteFactory = $ quoteFactory ;
77
50
}
78
51
79
52
/**
80
- * { @inheritdoc}
53
+ * @inheritdoc
81
54
*/
82
55
public function apply ()
83
56
{
@@ -96,32 +69,12 @@ public function apply()
96
69
*/
97
70
public function fillQuoteAddressIdInSalesOrderAddress (ModuleDataSetupInterface $ setup )
98
71
{
99
- $ addressTable = $ setup ->getTable ('sales_order_address ' );
100
- $ updateOrderAddress = $ setup ->getConnection ()
101
- ->select ()
102
- ->joinInner (
103
- ['sales_order ' => $ setup ->getTable ('sales_order ' )],
104
- $ addressTable . '.parent_id = sales_order.entity_id ' ,
105
- ['quote_address_id ' => 'quote_address.address_id ' ]
106
- )
107
- ->joinInner (
108
- ['quote_address ' => $ setup ->getTable ('quote_address ' )],
109
- 'sales_order.quote_id = quote_address.quote_id
110
- AND ' . $ addressTable . '.address_type = quote_address.address_type ' ,
111
- []
112
- )
113
- ->where (
114
- $ addressTable . '.quote_address_id IS NULL '
115
- );
116
- $ updateOrderAddress = $ setup ->getConnection ()->updateFromSelect (
117
- $ updateOrderAddress ,
118
- $ addressTable
119
- );
120
- $ setup ->getConnection ()->query ($ updateOrderAddress );
72
+ $ this ->fillQuoteAddressIdInSalesOrderAddressByType ($ setup , Address::TYPE_SHIPPING );
73
+ $ this ->fillQuoteAddressIdInSalesOrderAddressByType ($ setup , Address::TYPE_BILLING );
121
74
}
122
75
123
76
/**
124
- * { @inheritdoc}
77
+ * @inheritdoc
125
78
*/
126
79
public static function getDependencies ()
127
80
{
@@ -131,18 +84,107 @@ public static function getDependencies()
131
84
}
132
85
133
86
/**
134
- * { @inheritdoc}
87
+ * @inheritdoc
135
88
*/
136
89
public static function getVersion ()
137
90
{
138
91
return '2.0.8 ' ;
139
92
}
140
93
141
94
/**
142
- * { @inheritdoc}
95
+ * @inheritdoc
143
96
*/
144
97
public function getAliases ()
145
98
{
146
99
return [];
147
100
}
101
+
102
+ /**
103
+ * Fill quote_address_id in sales_order_address by type.
104
+ *
105
+ * @param ModuleDataSetupInterface $setup
106
+ * @param string $addressType
107
+ * @throws \Zend_Db_Statement_Exception
108
+ */
109
+ private function fillQuoteAddressIdInSalesOrderAddressByType (ModuleDataSetupInterface $ setup , $ addressType )
110
+ {
111
+ $ salesConnection = $ setup ->getConnection ('sales ' );
112
+
113
+ $ orderTable = $ setup ->getTable ('sales_order ' , 'sales ' );
114
+ $ orderAddressTable = $ setup ->getTable ('sales_order_address ' , 'sales ' );
115
+
116
+ $ query = $ salesConnection
117
+ ->select ()
118
+ ->from (
119
+ ['sales_order_address ' => $ orderAddressTable ],
120
+ ['entity_id ' , 'address_type ' ]
121
+ )
122
+ ->joinInner (
123
+ ['sales_order ' => $ orderTable ],
124
+ 'sales_order_address.parent_id = sales_order.entity_id ' ,
125
+ ['quote_id ' => 'sales_order.quote_id ' ]
126
+ )
127
+ ->where ('sales_order_address.quote_address_id IS NULL ' )
128
+ ->where ('sales_order_address.address_type = ? ' , $ addressType )
129
+ ->order ('sales_order_address.entity_id ' );
130
+
131
+ $ batchSize = 5000 ;
132
+ $ result = $ salesConnection ->query ($ query );
133
+ $ count = $ result ->rowCount ();
134
+ $ batches = ceil ($ count / $ batchSize );
135
+
136
+ for ($ batch = $ batches ; $ batch > 0 ; $ batch --) {
137
+ $ query ->limitPage ($ batch , $ batchSize );
138
+ $ result = $ salesConnection ->fetchAssoc ($ query );
139
+
140
+ $ this ->fillQuoteAddressIdInSalesOrderAddressProcessBatch ($ setup , $ result , $ addressType );
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Process filling quote_address_id in sales_order_address in batch.
146
+ *
147
+ * @param ModuleDataSetupInterface $setup
148
+ * @param array $orderAddresses
149
+ * @param string $addressType
150
+ */
151
+ private function fillQuoteAddressIdInSalesOrderAddressProcessBatch (
152
+ ModuleDataSetupInterface $ setup ,
153
+ array $ orderAddresses ,
154
+ $ addressType
155
+ ) {
156
+ $ salesConnection = $ setup ->getConnection ('sales ' );
157
+ $ quoteConnection = $ setup ->getConnection ('checkout ' );
158
+
159
+ $ quoteAddressTable = $ setup ->getTable ('quote_address ' , 'checkout ' );
160
+ $ quoteTable = $ setup ->getTable ('quote ' , 'checkout ' );
161
+ $ salesOrderAddressTable = $ setup ->getTable ('sales_order_address ' , 'sales ' );
162
+
163
+ $ query = $ quoteConnection
164
+ ->select ()
165
+ ->from (
166
+ ['quote_address ' => $ quoteAddressTable ],
167
+ ['quote_id ' , 'address_id ' ]
168
+ )
169
+ ->joinInner (
170
+ ['quote ' => $ quoteTable ],
171
+ 'quote_address.quote_id = quote.entity_id ' ,
172
+ []
173
+ )
174
+ ->where ('quote.entity_id in (?) ' , array_column ($ orderAddresses , 'quote_id ' ))
175
+ ->where ('address_type = ? ' , $ addressType );
176
+
177
+ $ quoteAddresses = $ quoteConnection ->fetchAssoc ($ query );
178
+
179
+ foreach ($ orderAddresses as $ orderAddress ) {
180
+ $ bind = [
181
+ 'quote_address_id ' => $ quoteAddresses [$ orderAddress ['quote_id ' ]]['address_id ' ] ?? null ,
182
+ ];
183
+ $ where = [
184
+ 'entity_id = ? ' => $ orderAddress ['entity_id ' ]
185
+ ];
186
+
187
+ $ salesConnection ->update ($ salesOrderAddressTable , $ bind , $ where );
188
+ }
189
+ }
148
190
}
0 commit comments