@@ -47,29 +47,21 @@ public function __construct(
47
47
*/
48
48
private function process (int $ storeId , string $ table ): void
49
49
{
50
- $ connection = $ this ->resourceConnection ->getConnection ();
51
50
$ catalogProductTable = $ this ->resourceConnection ->getTableName ($ table );
52
- $ select = $ connection ->select ()
53
- ->from ($ catalogProductTable , ['value_id ' , 'attribute_id ' , 'row_id ' ])
54
- ->where ('store_id = ? ' , $ storeId );
55
- $ catalogProducts = $ connection ->fetchAll ($ select );
51
+
52
+ $ catalogProducts = $ this ->getCatalogProducts ($ table , $ storeId );
53
+ $ rowIds = [];
54
+ $ attributeIds = [];
55
+ $ valueIds = [];
56
56
try {
57
57
if ($ catalogProducts ) {
58
58
foreach ($ catalogProducts as $ catalogProduct ) {
59
- $ connection ->delete (
60
- $ table ,
61
- [
62
- 'store_id = ? ' => Store::DEFAULT_STORE_ID ,
63
- 'attribute_id = ? ' => $ catalogProduct ['attribute_id ' ],
64
- 'row_id = ? ' => $ catalogProduct ['row_id ' ]
65
- ]
66
- );
67
- $ connection ->update (
68
- $ table ,
69
- ['store_id ' => Store::DEFAULT_STORE_ID ],
70
- ['value_id = ? ' => $ catalogProduct ['value_id ' ]]
71
- );
59
+ $ rowIds [] = $ catalogProduct ['row_id ' ];
60
+ $ attributeIds [] = $ catalogProduct ['attribute_id ' ];
61
+ $ valueIds [] = $ catalogProduct ['value_id ' ];
72
62
}
63
+ $ this ->massDelete ($ catalogProductTable , $ attributeIds , $ rowIds );
64
+ $ this ->massUpdate ($ catalogProductTable , $ valueIds );
73
65
}
74
66
} catch (LocalizedException $ e ) {
75
67
throw new CouldNotSaveException (
@@ -111,4 +103,61 @@ public function migrateCatalogCategoryAndProductTables(int $storeId): void
111
103
$ connection ->rollBack ();
112
104
}
113
105
}
106
+
107
+ /**
108
+ * Delete default store related products
109
+ *
110
+ * @param $catalogProductTable
111
+ * @param array $attributeIds
112
+ * @param array $rowIds
113
+ * @return void
114
+ */
115
+ private function massDelete ($ catalogProductTable , array $ attributeIds , array $ rowIds ): void
116
+ {
117
+ $ connection = $ this ->resourceConnection ->getConnection ();
118
+
119
+ $ connection ->delete (
120
+ $ catalogProductTable ,
121
+ [
122
+ 'store_id = ? ' => Store::DEFAULT_STORE_ID ,
123
+ 'attribute_id IN(?) ' => $ attributeIds ,
124
+ 'row_id IN(?) ' => $ rowIds
125
+ ]
126
+ );
127
+ }
128
+
129
+ /**
130
+ * Update default store related products
131
+ *
132
+ * @param $catalogProductTable
133
+ * @param array $valueIds
134
+ * @return void
135
+ */
136
+ private function massUpdate ($ catalogProductTable , array $ valueIds ): void
137
+ {
138
+ $ connection = $ this ->resourceConnection ->getConnection ();
139
+
140
+ $ connection ->update (
141
+ $ catalogProductTable ,
142
+ ['store_id ' => Store::DEFAULT_STORE_ID ],
143
+ ['value_id IN(?) ' => $ valueIds ]
144
+ );
145
+ }
146
+
147
+ /**
148
+ * Get list of products
149
+ *
150
+ * @param string $table
151
+ * @param int $storeId
152
+ * @return array
153
+ */
154
+ private function getCatalogProducts (string $ table , int $ storeId ): array
155
+ {
156
+ $ connection = $ this ->resourceConnection ->getConnection ();
157
+ $ catalogProductTable = $ this ->resourceConnection ->getTableName ($ table );
158
+ $ select = $ connection ->select ()
159
+ ->from ($ catalogProductTable , ['value_id ' , 'attribute_id ' , 'row_id ' ])
160
+ ->where ('store_id = ? ' , $ storeId );
161
+ return $ connection ->fetchAll ($ select );
162
+ }
114
163
}
0 commit comments