1313
1414class WishlistItemPermissionsCollectionProcessor
1515{
16+ /**
17+ * @var array
18+ */
19+ private array $ validProductIds = [];
20+
1621 /**
1722 * @param ProductRepositoryInterface $productRepository
1823 * @param SearchCriteriaBuilder $searchCriteriaBuilder
@@ -33,27 +38,38 @@ public function __construct(
3338 public function execute (Collection $ collection ): Collection
3439 {
3540 $ items = $ collection ->getItems ();
36- $ productIds = array_map (static fn ($ item ) => $ item ->getProductId (), $ items );
37- $ searchCriteria = $ this ->searchCriteriaBuilder
38- ->addFilter ('entity_id ' , $ productIds , 'in ' )
39- ->create ();
40- $ productCollection = $ this ->productRepository ->getList ($ searchCriteria );
41- $ products = array_combine (
42- array_map (fn ($ p ) => $ p ->getId (), $ productCollection ->getItems ()),
43- $ productCollection ->getItems ()
44- );
45-
46- $ validItems = [];
47- $ collection ->removeAllItems ();
48- foreach ($ items as $ item ) {
49- if (!isset ($ products [$ item ->getProductId ()]) || $ products [$ item ->getProductId ()]->getIsHidden ()) {
50- continue ;
41+ if (empty ($ items )) {
42+ return $ collection ;
43+ }
44+ $ productIds = $ collection ->getColumnValues ('product_id ' );
45+ $ cacheKey = sha1 (implode ("- " , $ productIds ));
46+ if (!isset ($ this ->validProductIds [$ cacheKey ])) {
47+ $ searchCriteria = $ this ->searchCriteriaBuilder
48+ ->addFilter ('entity_id ' , $ productIds , 'in ' )
49+ ->create ();
50+ $ productCollection = $ this ->productRepository ->getList ($ searchCriteria );
51+ $ products = array_combine (
52+ array_map (fn ($ p ) => $ p ->getId (), $ productCollection ->getItems ()),
53+ $ productCollection ->getItems ()
54+ );
55+
56+ $ validItems = [];
57+ foreach ($ items as $ item ) {
58+ if (!isset ($ products [$ item ->getProductId ()]) || $ products [$ item ->getProductId ()]->getIsHidden ()) {
59+ continue ;
60+ }
61+ $ validItems [] = (int )$ item ->getProductId ();
5162 }
52- $ validItems [] = $ item ->getProductId ();
53- $ collection ->addItem ($ item );
63+ $ this ->validProductIds [$ cacheKey ] = $ validItems ;
5464 }
55- if (!empty ($ validItems )) {
56- $ collection ->addFieldToFilter ('main_table.product_id ' , ['in ' => $ validItems ]);
65+
66+ if (!empty ($ this ->validProductIds [$ cacheKey ])) {
67+ $ collection ->addFilter (
68+ 'main_table.product_id ' ,
69+ 'main_table.product_id IN( ' . implode (", " , $ this ->validProductIds [$ cacheKey ]) . ') ' ,
70+ 'string '
71+ );
72+ $ collection ->clear ();
5773 }
5874
5975 return $ collection ;
0 commit comments