Skip to content

Commit 68d34fe

Browse files
committed
Option to include product categories to data layer
1 parent aac045e commit 68d34fe

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

Model/AbstractDataLayer.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Catalog\Model\Product;
1515
use Magento\Framework\Exception\NoSuchEntityException;
1616
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\App\ObjectManager;
1720

1821
class AbstractDataLayer
1922
{
@@ -32,6 +35,16 @@ class AbstractDataLayer
3235
*/
3336
protected $categoryRepository;
3437

38+
/**
39+
* @var RequestInterface
40+
*/
41+
protected $request;
42+
43+
/**
44+
* @var Registry
45+
*/
46+
protected $registry;
47+
3548
/**
3649
* AbstractDataLayer constructor.
3750
*
@@ -42,11 +55,19 @@ class AbstractDataLayer
4255
public function __construct(
4356
Config $config,
4457
StoreManagerInterface $storeManager,
45-
CategoryRepositoryInterface $categoryRepository
58+
CategoryRepositoryInterface $categoryRepository,
59+
RequestInterface $request = null,
60+
Registry $registry = null
4661
) {
4762
$this->config = $config;
4863
$this->storeManager = $storeManager;
4964
$this->categoryRepository = $categoryRepository;
65+
$this->request = $request ?: ObjectManager::getInstance()->get(
66+
RequestInterface::class
67+
);
68+
$this->registry = $registry ?: ObjectManager::getInstance()->get(
69+
Registry::class
70+
);
5071
}
5172

5273
/**
@@ -58,13 +79,12 @@ public function __construct(
5879
*/
5980
protected function getCategoryNames(Product $product): array
6081
{
61-
/* Temporary disable displaying category names,
62-
* as it is optional and slows down the website speed
63-
*/
64-
return [];
65-
6682
$result = [];
6783

84+
if (!$this->config->getCategoriesAttribute()) {
85+
return $result;
86+
}
87+
6888
if ($productCategory = $this->getCategoryByProduct($product)) {
6989
$categoryIds = $productCategory->getPathIds();
7090
$number = 1;
@@ -91,8 +111,13 @@ protected function getCategoryNames(Product $product): array
91111
*/
92112
private function getCategoryByProduct(Product $product): ?CategoryInterface
93113
{
94-
$productCategory = null;
114+
if ('catalog_category_product' == $this->request->getFullActionName()) {
115+
if ($category = $this->registry->registry('current_category')) {
116+
return $category;
117+
}
118+
}
95119

120+
$productCategory = null;
96121
$categoryIds = $product->getCategoryIds();
97122

98123
if ($categoryIds) {

Model/Config.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Config
3232
*/
3333
public const XML_PATH_ATTRIBUTES_PRODUCT = 'mfgoogletagmanager/attributes/product';
3434
public const XML_PATH_ATTRIBUTES_BRAND = 'mfgoogletagmanager/attributes/brand';
35+
public const XML_PATH_ATTRIBUTES_CATEGORIES = 'mfgoogletagmanager/attributes/categories';
3536

3637
/**
3738
* @var ScopeConfigInterface
@@ -138,6 +139,17 @@ public function getBrandAttribute(string $storeId = null): string
138139
return (string)$this->getConfig(self::XML_PATH_ATTRIBUTES_BRAND, $storeId);
139140
}
140141

142+
/**
143+
* Retrieve Magento product categories
144+
*
145+
* @param string|null $storeId
146+
* @return string
147+
*/
148+
public function getCategoriesAttribute(string $storeId = null): string
149+
{
150+
return (string)$this->getConfig(self::XML_PATH_ATTRIBUTES_CATEGORIES, $storeId);
151+
}
152+
141153
/**
142154
* Retrieve store config value
143155
*

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ height="0" width="0" style="display:none;visibility:hid
9797
<label>Brand Identifier</label>
9898
<source_model>Magefan\GoogleTagManager\Model\Config\Source\BrandAttribute</source_model>
9999
</field>
100+
<field id="categories" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
101+
<label>Include Categories</label>
102+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
103+
<comment><![CDATA[<strong class="colorRed">Warning!</strong> Enabling this option may cause the performance impact.]]></comment>
104+
</field>
100105
</group>
101106
<group id="container" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
102107
<label>Export Container</label>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<attributes>
2424
<product>sku</product>
2525
<brand/>
26+
<categories/>
2627
</attributes>
2728
</mfgoogletagmanager>
2829
</default>

0 commit comments

Comments
 (0)