8
8
namespace Magento \ConfigurableProduct \Plugin \CatalogWidget \Block \Product ;
9
9
10
10
use Magento \Catalog \Model \Product ;
11
- use Magento \Catalog \Model \Product \Visibility ;
12
11
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
13
- use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
14
12
use Magento \CatalogWidget \Block \Product \ProductsList ;
15
13
use Magento \Framework \App \ResourceConnection ;
14
+ use Magento \Framework \DB \Select ;
16
15
use Magento \Framework \EntityManager \MetadataPool ;
17
16
use Magento \Framework \Exception \LocalizedException ;
18
17
19
18
class ProductsListPlugin
20
19
{
21
- /**
22
- * @var CollectionFactory
23
- */
24
- private CollectionFactory $ productCollectionFactory ;
25
-
26
- /**
27
- * @var Visibility
28
- */
29
- private Visibility $ catalogProductVisibility ;
30
-
31
20
/**
32
21
* @var ResourceConnection
33
22
*/
@@ -39,19 +28,13 @@ class ProductsListPlugin
39
28
private MetadataPool $ metadataPool ;
40
29
41
30
/**
42
- * @param CollectionFactory $productCollectionFactory
43
- * @param Visibility $catalogProductVisibility
44
31
* @param ResourceConnection $resource
45
32
* @param MetadataPool $metadataPool
46
33
*/
47
34
public function __construct (
48
- CollectionFactory $ productCollectionFactory ,
49
- Visibility $ catalogProductVisibility ,
50
35
ResourceConnection $ resource ,
51
36
MetadataPool $ metadataPool
52
37
) {
53
- $ this ->productCollectionFactory = $ productCollectionFactory ;
54
- $ this ->catalogProductVisibility = $ catalogProductVisibility ;
55
38
$ this ->resource = $ resource ;
56
39
$ this ->metadataPool = $ metadataPool ;
57
40
}
@@ -95,18 +78,36 @@ public function afterCreateCollection(ProductsList $subject, Collection $result)
95
78
->where ('link_table.product_id IN (?) ' , $ searchProducts )
96
79
);
97
80
98
- $ configurableProductCollection = $ this ->productCollectionFactory ->create ();
99
- $ configurableProductCollection ->setVisibility ($ this ->catalogProductVisibility ->getVisibleInCatalogIds ());
100
- $ configurableProductCollection ->addIdFilter ($ productIds );
101
-
102
- /** @var Product $item */
103
- foreach ($ configurableProductCollection ->getItems () as $ item ) {
104
- if (false === in_array ($ item ->getId (), $ currentIds )) {
105
- $ result ->addItem ($ item ->load ($ item ->getId ()));
106
- }
81
+ if (empty ($ productIds )) {
82
+ return $ result ;
107
83
}
84
+ $ this ->addParentProductsToSelect ($ result , $ productIds );
108
85
}
109
86
110
87
return $ result ;
111
88
}
89
+
90
+ /**
91
+ * @param Collection $result The product collection
92
+ * @param array $productIds Array of parent product IDs to include
93
+ * @return void
94
+ */
95
+ protected function addParentProductsToSelect (Collection $ result , array $ productIds ): void
96
+ {
97
+ $ connection = $ this ->resource ->getConnection ();
98
+ $ select = $ result ->getSelect ();
99
+ $ originalWhere = $ select ->getPart (Select::WHERE );
100
+ if (!empty ($ originalWhere )) {
101
+ $ originalWhere = array_filter ($ originalWhere , 'strlen ' );
102
+ }
103
+ $ parentCondition = $ connection ->quoteInto ('e.entity_id IN (?) ' , $ productIds );
104
+
105
+ $ newWhere = [];
106
+ if (!empty ($ originalWhere )) {
107
+ $ newWhere [] = '( ' . implode (' ' , $ originalWhere ) . ') ' ;
108
+ }
109
+ $ newWhere [] = $ parentCondition ;
110
+ $ select ->reset (Select::WHERE );
111
+ $ select ->where (new \Zend_Db_Expr (implode (' OR ' , $ newWhere )));
112
+ }
112
113
}
0 commit comments