Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions DataLayer/Tag/Category/CategorySize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
54 changes: 0 additions & 54 deletions Plugin/GetProductsFromCategoryBlockPlugin.php

This file was deleted.

25 changes: 0 additions & 25 deletions Test/Integration/Plugin/GetProductsFromCategoryBlockPluginTest.php

This file was deleted.

59 changes: 58 additions & 1 deletion Util/GetCurrentCategoryProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
37 changes: 37 additions & 0 deletions Util/GetCurrentProductListCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Tagging\GTM\Util;

use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Data\Collection\AbstractDb;

class GetCurrentProductListCollection
{
private Resolver $layerResolver;

/**
* @param Resolver $layerResolver
*/
public function __construct(Resolver $layerResolver)
{
$this->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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 0 additions & 4 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<plugin name="Tagging_GTM::addAdditionalDataToCartSection" type="Tagging\GTM\Plugin\AddDataToCartSection"/>
</type>

<type name="Magento\Catalog\Block\Product\ListProduct">
<plugin name="Tagging_GTM::getProductsFromCategoryBlockPlugin" type="Tagging\GTM\Plugin\GetProductsFromCategoryBlockPlugin"/>
</type>

<type name="Magento\CatalogSearch\Controller\Result\Index">
<plugin name="Tagging_GTM::TriggerViewSearchResultDataLayerEvent" type="Tagging\GTM\Plugin\TriggerViewSearchResultDataLayerEvent"/>
</type>
Expand Down
Loading