From eaa1555e9e26bd2b448047109d4356d37074b4f6 Mon Sep 17 00:00:00 2001 From: Victor Kloeppel Date: Tue, 8 Sep 2026 11:14:38 +0200 Subject: [PATCH 1/3] changed event on which to get the item collection --- Observer/CollectProductListProducts.php | 70 +++++++++++++++++++ Plugin/GetProductsFromCategoryBlockPlugin.php | 54 -------------- ...GetProductsFromCategoryBlockPluginTest.php | 25 ------- etc/frontend/di.xml | 4 -- etc/frontend/events.xml | 4 ++ 5 files changed, 74 insertions(+), 83 deletions(-) create mode 100644 Observer/CollectProductListProducts.php delete mode 100644 Plugin/GetProductsFromCategoryBlockPlugin.php delete mode 100644 Test/Integration/Plugin/GetProductsFromCategoryBlockPluginTest.php diff --git a/Observer/CollectProductListProducts.php b/Observer/CollectProductListProducts.php new file mode 100644 index 00000000..bedccfd7 --- /dev/null +++ b/Observer/CollectProductListProducts.php @@ -0,0 +1,70 @@ +getCurrentCategoryProducts = $getCurrentCategoryProducts; + $this->categorySize = $categorySize; + } + + /** + * @param Observer $observer + * @return void + */ + public function execute(Observer $observer) + { + $collection = $observer->getData('collection'); + if (!$collection instanceof Collection) { + return; + } + + $counter = 0; + foreach ($collection->getItems() as $product) { + if ($counter >= self::MAXIMUM_PRODUCTS) { + break; + } + + if (!$product instanceof ProductInterface) { + continue; + } + + $this->getCurrentCategoryProducts->addProduct($product); + $counter++; + } + + $this->categorySize->setSize($collection->count()); + } +} diff --git a/Plugin/GetProductsFromCategoryBlockPlugin.php b/Plugin/GetProductsFromCategoryBlockPlugin.php deleted file mode 100644 index 4bc4c1b7..00000000 --- a/Plugin/GetProductsFromCategoryBlockPlugin.php +++ /dev/null @@ -1,54 +0,0 @@ -categorySize = $categorySize; - $this->getCurrentCategoryProducts = $getCurrentCategoryProducts; - $this->config = $config; - } - - /** - * @param ListProduct $listProductBlock - * @param AbstractCollection $collection - * @return AbstractCollection - */ - public function afterGetLoadedProductCollection( - ListProduct $listProductBlock, - AbstractCollection $collection - ): AbstractCollection { - $maximumCategoryProducts = 50; - $i = 0; - foreach ($collection as $product) { - if ($i > $maximumCategoryProducts) { - break; - } - - $this->getCurrentCategoryProducts->addProduct($product); - $i++; - } - - $this->categorySize->setSize($collection->count()); - return $collection; - } -} diff --git a/Test/Integration/Plugin/GetProductsFromCategoryBlockPluginTest.php b/Test/Integration/Plugin/GetProductsFromCategoryBlockPluginTest.php deleted file mode 100644 index 83fcf204..00000000 --- a/Test/Integration/Plugin/GetProductsFromCategoryBlockPluginTest.php +++ /dev/null @@ -1,25 +0,0 @@ -assertInterceptorPluginIsRegistered( - ListProductBlock::class, - GetProductsFromCategoryBlockPlugin::class, - 'Tagging_GTM::getProductsFromCategoryBlockPlugin' - ); - } -} diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index cdfe4fbc..506a2fce 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -12,10 +12,6 @@ - - - - diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml index aa48e10c..c5b236d8 100644 --- a/etc/frontend/events.xml +++ b/etc/frontend/events.xml @@ -4,6 +4,10 @@ + + + + From 6264e6f5ec4357ebd9a8ea3df28fa2a6e858b091 Mon Sep 17 00:00:00 2001 From: Victor Kloeppel Date: Wed, 9 Sep 2026 10:54:34 +0200 Subject: [PATCH 2/3] Collect category products without hooking the product list block --- DataLayer/Tag/Category/CategorySize.php | 24 +++++++- Observer/CollectProductListProducts.php | 70 ------------------------ Util/GetCurrentCategoryProducts.php | 59 +++++++++++++++++++- Util/GetCurrentProductListCollection.php | 37 +++++++++++++ etc/frontend/events.xml | 4 -- 5 files changed, 117 insertions(+), 77 deletions(-) delete mode 100644 Observer/CollectProductListProducts.php create mode 100644 Util/GetCurrentProductListCollection.php diff --git a/DataLayer/Tag/Category/CategorySize.php b/DataLayer/Tag/Category/CategorySize.php index 69bc8aff..cf351ab3 100644 --- a/DataLayer/Tag/Category/CategorySize.php +++ b/DataLayer/Tag/Category/CategorySize.php @@ -3,10 +3,24 @@ namespace Tagging\GTM\DataLayer\Tag\Category; use Tagging\GTM\Api\Data\TagInterface; +use Tagging\GTM\Util\GetCurrentProductListCollection; class CategorySize implements TagInterface { - private int $size = 0; + private GetCurrentProductListCollection $getCurrentProductListCollection; + + /** + * @var int|null + */ + private $size = null; + + /** + * @param GetCurrentProductListCollection $getCurrentProductListCollection + */ + public function __construct(GetCurrentProductListCollection $getCurrentProductListCollection) + { + $this->getCurrentProductListCollection = $getCurrentProductListCollection; + } /** * @param int $size @@ -22,6 +36,12 @@ public function setSize(int $size = 0) */ public function get(): int { - return $this->size; + if ($this->size !== null) { + return $this->size; + } + + $collection = $this->getCurrentProductListCollection->get(); + + return $collection === null ? 0 : count($collection->getItems()); } } diff --git a/Observer/CollectProductListProducts.php b/Observer/CollectProductListProducts.php deleted file mode 100644 index bedccfd7..00000000 --- a/Observer/CollectProductListProducts.php +++ /dev/null @@ -1,70 +0,0 @@ -getCurrentCategoryProducts = $getCurrentCategoryProducts; - $this->categorySize = $categorySize; - } - - /** - * @param Observer $observer - * @return void - */ - public function execute(Observer $observer) - { - $collection = $observer->getData('collection'); - if (!$collection instanceof Collection) { - return; - } - - $counter = 0; - foreach ($collection->getItems() as $product) { - if ($counter >= self::MAXIMUM_PRODUCTS) { - break; - } - - if (!$product instanceof ProductInterface) { - continue; - } - - $this->getCurrentCategoryProducts->addProduct($product); - $counter++; - } - - $this->categorySize->setSize($collection->count()); - } -} diff --git a/Util/GetCurrentCategoryProducts.php b/Util/GetCurrentCategoryProducts.php index 5e5bffb7..8aaaf600 100644 --- a/Util/GetCurrentCategoryProducts.php +++ b/Util/GetCurrentCategoryProducts.php @@ -6,18 +6,75 @@ class GetCurrentCategoryProducts { + private const MAXIMUM_PRODUCTS = 50; + + private GetCurrentProductListCollection $getCurrentProductListCollection; + + /** + * @var ProductInterface[] + */ private $products = []; + /** + * @var bool + */ + private $collected = false; + + /** + * @param GetCurrentProductListCollection $getCurrentProductListCollection + */ + public function __construct(GetCurrentProductListCollection $getCurrentProductListCollection) + { + $this->getCurrentProductListCollection = $getCurrentProductListCollection; + } + + /** + * @param ProductInterface $product + * @return void + */ public function addProduct(ProductInterface $product) { $this->products[$product->getId()] = $product; + $this->collected = true; } /** - * @return array + * @return ProductInterface[] */ public function getProducts(): array { + if ($this->collected === false) { + $this->collected = true; + $this->collectFromCurrentListing(); + } + return $this->products; } + + /** + * Read the products of the current listing, capped so a large page size cannot blow up the + * dataLayer. Does nothing while the listing collection is still unloaded, so the toolbar keeps + * control over the page size - see GetCurrentProductListCollection. + * + * @return void + */ + private function collectFromCurrentListing(): void + { + $collection = $this->getCurrentProductListCollection->get(); + if ($collection === null) { + return; + } + + foreach ($collection->getItems() as $product) { + if (count($this->products) >= self::MAXIMUM_PRODUCTS) { + break; + } + + if (!$product instanceof ProductInterface) { + continue; + } + + $this->products[$product->getId()] = $product; + } + } } diff --git a/Util/GetCurrentProductListCollection.php b/Util/GetCurrentProductListCollection.php new file mode 100644 index 00000000..dcacfb94 --- /dev/null +++ b/Util/GetCurrentProductListCollection.php @@ -0,0 +1,37 @@ +layerResolver = $layerResolver; + } + + /** + * @return AbstractDb|null The loaded listing collection, or null when there is none (yet) + */ + public function get(): ?AbstractDb + { + try { + $collection = $this->layerResolver->get()->getProductCollection(); + } catch (\Throwable $e) { + return null; + } + + if (!$collection instanceof AbstractDb || !$collection->isLoaded()) { + return null; + } + + return $collection; + } +} diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml index c5b236d8..aa48e10c 100644 --- a/etc/frontend/events.xml +++ b/etc/frontend/events.xml @@ -4,10 +4,6 @@ - - - - From dbabaf64fd5a43a34e529328e5e482d7d2ed10e2 Mon Sep 17 00:00:00 2001 From: Victor Kloeppel Date: Thu, 10 Sep 2026 10:19:35 +0200 Subject: [PATCH 3/3] Bumped version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c29da440..6565b796 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tagginggroup/gtm", - "version": "1.8.0", + "version": "1.9.0", "license": "OSL-3.0", "type": "magento2-module", "description": "AdPage tagging integration for Magento 2",