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/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/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/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", 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 @@ - - - -