@@ -138,7 +138,9 @@ private function getProductIdsByActions(array $actions)
138
138
$ productIds = [];
139
139
140
140
foreach ($ actions as $ action ) {
141
- $ productIds [] = $ action ['product_id ' ];
141
+ if (isset ($ action ['product_id ' ]) && is_int ($ action ['product_id ' ])) {
142
+ $ productIds [] = $ action ['product_id ' ];
143
+ }
142
144
}
143
145
144
146
return $ productIds ;
@@ -159,33 +161,37 @@ public function syncActions(array $productsData, $typeId)
159
161
$ customerId = $ this ->session ->getCustomerId ();
160
162
$ visitorId = $ this ->visitor ->getId ();
161
163
$ collection = $ this ->getActionsByType ($ typeId );
162
- $ collection ->addFieldToFilter ('product_id ' , $ this ->getProductIdsByActions ($ productsData ));
163
-
164
- /**
165
- * Note that collection is also filtered by visitor id and customer id
166
- * This collection shouldnt be flushed when visitor has products and then login
167
- * It can remove only products for visitor, or only products for customer
168
- *
169
- * ['product_id' => 'added_at']
170
- * @var ProductFrontendActionInterface $item
171
- */
172
- foreach ($ collection as $ item ) {
173
- $ this ->entityManager ->delete ($ item );
174
- }
175
-
176
- foreach ($ productsData as $ productId => $ productData ) {
177
- /** @var ProductFrontendActionInterface $action */
178
- $ action = $ this ->productFrontendActionFactory ->create ([
179
- 'data ' => [
180
- 'visitor_id ' => $ customerId ? null : $ visitorId ,
181
- 'customer_id ' => $ this ->session ->getCustomerId (),
182
- 'added_at ' => $ productData ['added_at ' ],
183
- 'product_id ' => $ productId ,
184
- 'type_id ' => $ typeId
185
- ]
186
- ]);
187
-
188
- $ this ->entityManager ->save ($ action );
164
+ $ productIds = $ this ->getProductIdsByActions ($ productsData );
165
+
166
+ if ($ productIds ) {
167
+ $ collection ->addFieldToFilter ('product_id ' , $ productIds );
168
+
169
+ /**
170
+ * Note that collection is also filtered by visitor id and customer id
171
+ * This collection shouldnt be flushed when visitor has products and then login
172
+ * It can remove only products for visitor, or only products for customer
173
+ *
174
+ * ['product_id' => 'added_at']
175
+ * @var ProductFrontendActionInterface $item
176
+ */
177
+ foreach ($ collection as $ item ) {
178
+ $ this ->entityManager ->delete ($ item );
179
+ }
180
+
181
+ foreach ($ productsData as $ productId => $ productData ) {
182
+ /** @var ProductFrontendActionInterface $action */
183
+ $ action = $ this ->productFrontendActionFactory ->create ([
184
+ 'data ' => [
185
+ 'visitor_id ' => $ customerId ? null : $ visitorId ,
186
+ 'customer_id ' => $ this ->session ->getCustomerId (),
187
+ 'added_at ' => $ productData ['added_at ' ],
188
+ 'product_id ' => $ productId ,
189
+ 'type_id ' => $ typeId
190
+ ]
191
+ ]);
192
+
193
+ $ this ->entityManager ->save ($ action );
194
+ }
189
195
}
190
196
}
191
197
0 commit comments