Skip to content

Commit 7935697

Browse files
authored
Merge pull request #46 from stape-io/release/v1.0.26
Release/v1.0.26
2 parents daeca84 + 2b90fdf commit 7935697

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
v1.0.26
44
- removed unneeded CSP rules
5+
- added option to configure collection size for datalayer
56

67
v1.0.24
78
- added caching of cookie domain when generating _sbp cookie

Model/ConfigProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class ConfigProvider
5353
*/
5454
public const XPATH_USER_DATA_ENABLED = 'stape_gtm/datalayer/userdata';
5555

56+
/*
57+
* XPATH for collection size on category page to be pushed to datalayer
58+
*/
59+
public const XPATH_COLLECTION_SIZE = 'stape_gtm/datalayer/collection_size';
60+
5661
/*
5762
* XPATH to check if webhooks are enabled
5863
*/
@@ -274,4 +279,15 @@ public function getDomainListUrl()
274279
{
275280
return $this->scopeConfig->getValue(self::XPATH_DOMAIN_LIST_URL);
276281
}
282+
283+
/**
284+
* Retrieve allowed collection size
285+
*
286+
* @param string|null $scopeCode
287+
* @return int
288+
*/
289+
public function getCollectionSize($scopeCode = null): int
290+
{
291+
return (int) $this->scopeConfig->getValue(self::XPATH_COLLECTION_SIZE, ScopeInterface::SCOPE_STORE, $scopeCode);
292+
}
277293
}

ViewModel/Category.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Magento\Catalog\Model\Layer;
66
use Magento\Catalog\Model\Layer\Resolver;
7+
use Magento\Framework\Event\ManagerInterface;
78
use Magento\Framework\Pricing\PriceCurrencyInterface;
89
use Magento\Framework\Serialize\Serializer\Json;
910
use Magento\Framework\View\Element\Block\ArgumentInterface;
1011
use Magento\Framework\View\Layout;
1112
use Magento\Store\Model\StoreManagerInterface;
13+
use Stape\Gtm\Model\ConfigProvider;
1214

1315
class 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 */

etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ This feature is available only if you use <a href="https://stape.io/gtm-server-h
5353
<label>Add user data to Data Layer events</label>
5454
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
5555
</field>
56+
<field id="collection_size" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label">
57+
<label>Product Collection Limit</label>
58+
<validate>validate-integer</validate>
59+
</field>
5660
</group>
5761
<group id="webhooks" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
5862
<label>Webhooks</label>

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<domain_list>
77
<url><![CDATA[https://publicsuffix.org/list/public_suffix_list.dat]]></url>
88
</domain_list>
9+
<datalayer>
10+
<collection_size>30</collection_size>
11+
</datalayer>
912
</stape_gtm>
1013
</default>
1114
</config>

0 commit comments

Comments
 (0)