44
55use Magento \Catalog \Model \Layer ;
66use Magento \Catalog \Model \Layer \Resolver ;
7+ use Magento \Framework \Event \ManagerInterface ;
78use Magento \Framework \Pricing \PriceCurrencyInterface ;
89use Magento \Framework \Serialize \Serializer \Json ;
910use Magento \Framework \View \Element \Block \ArgumentInterface ;
1011use Magento \Framework \View \Layout ;
1112use Magento \Store \Model \StoreManagerInterface ;
13+ use Stape \Gtm \Model \ConfigProvider ;
1214
1315class Category implements ArgumentInterface, DatalayerInterface
1416{
@@ -33,6 +35,16 @@ class Category implements ArgumentInterface, DatalayerInterface
3335 /** @var Layout $layout */
3436 private $ layout ;
3537
38+ /**
39+ * @var ConfigProvider $configProvider
40+ */
41+ private $ configProvider ;
42+
43+ /**
44+ * @var ManagerInterface $eventManager
45+ */
46+ private $ eventManager ;
47+
3648 /**
3749 * Define class dependencies
3850 *
@@ -41,19 +53,26 @@ class Category implements ArgumentInterface, DatalayerInterface
4153 * @param Resolver $layerResolver
4254 * @param PriceCurrencyInterface $priceCurrency
4355 * @param Layout $layout
56+ * @param ContextInterface $context
57+ * @param ConfigProvider $configProvider
58+ * @param ManagerInterface $eventManager
4459 */
4560 public function __construct (
4661 Json $ json ,
4762 StoreManagerInterface $ storeManager ,
4863 Resolver $ layerResolver ,
4964 PriceCurrencyInterface $ priceCurrency ,
50- Layout $ layout
65+ Layout $ layout ,
66+ ConfigProvider $ configProvider ,
67+ ManagerInterface $ eventManager
5168 ) {
5269 $ this ->json = $ json ;
5370 $ this ->storeManager = $ storeManager ;
5471 $ this ->layer = $ layerResolver ->get ();
5572 $ this ->priceCurrency = $ priceCurrency ;
5673 $ this ->layout = $ layout ;
74+ $ this ->configProvider = $ configProvider ;
75+ $ this ->eventManager = $ eventManager ;
5776 }
5877
5978 /**
@@ -70,17 +89,46 @@ private function getCategoryName()
7089 return '' ;
7190 }
7291
92+ /**
93+ * Retrieve collection
94+ *
95+ * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
96+ */
97+ private function prepareCollection ()
98+ {
99+ /** @var \Magento\Catalog\Block\Product\ListProduct $productList */
100+ $ productList = $ this ->layout ->createBlock (\Magento \Catalog \Block \Product \ListProduct::class);
101+ $ productCollection = $ this ->layer ->getProductCollection ();
102+
103+ // if collection is loaded there is some third-party/custom code loading it and conflicting the Stape module
104+ if ($ productCollection ->isLoaded ()) {
105+ return $ productCollection ;
106+ }
107+
108+ // cloning to avoid interfering with original product listing
109+ $ collection = clone $ productCollection ;
110+
111+ $ toolbar = $ productList ->getToolbarBlock ();
112+ $ toolbar ->setCollection ($ collection );
113+ $ collection ->setPageSize ($ this ->configProvider ->getCollectionSize ());
114+ $ collection ->setCurPage (1 );
115+
116+ $ this ->eventManager ->dispatch (
117+ 'catalog_block_product_list_collection ' ,
118+ ['collection ' => $ collection ]
119+ );
120+
121+ return $ collection ;
122+ }
123+
73124 /**
74125 * Preparing items
75126 *
76127 * @return array
77128 */
78129 public function prepareItems ()
79130 {
80- /** @var \Magento\Catalog\Block\Product\ListProduct $productList */
81- $ productList = $ this ->layout ->createBlock (\Magento \Catalog \Block \Product \ListProduct::class);
82- $ productList ->getToolbarBlock ()->setCollection ($ this ->layer ->getProductCollection ());
83- $ collection = $ productList ->getLoadedProductCollection ();
131+ $ collection = $ this ->prepareCollection ();
84132 $ items = [];
85133 $ index = 0 ;
86134 /** @var \Magento\Catalog\Model\Product $product */
0 commit comments