Skip to content

Commit

Permalink
Merge pull request #234 from marcinfr/issue-232-item-list-name-on-plp
Browse files Browse the repository at this point in the history
item_list_name on PLP
  • Loading branch information
jissereitsma authored Aug 2, 2024
2 parents a74ba18 + 0accf69 commit bc67ab2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ public function showViewMiniCartOnExpandOnly(): bool
return (bool)$this->getModuleConfigValue('view_cart_on_mini_cart_expand_only');
}

/**
* @return string
*/
public function getProductListValueOnCategory(): string
{
return (string)$this->getModuleConfigValue('product_list_value_on_category');
}

/**
* Return a configuration value
*
Expand Down
18 changes: 17 additions & 1 deletion DataLayer/Mapper/ProductDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yireo\GoogleTagManager2\DataLayer\Mapper;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Yireo\GoogleTagManager2\Api\Data\ProductTagInterface;
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\Model\Config\Source\ProductListValue;
use Yireo\GoogleTagManager2\Util\Attribute\GetAttributeValue;
use Yireo\GoogleTagManager2\Util\CategoryProvider;
use Yireo\GoogleTagManager2\Util\PriceFormatter;
Expand Down Expand Up @@ -72,7 +74,7 @@ public function mapByProduct(ProductInterface $product): array
}

try {
$category = $this->categoryProvider->getFirstByProduct($product);
$category = $this->getProductCategory($product);
$productData[$prefix . 'list_id'] = $category->getId();
$productData[$prefix . 'list_name'] = $category->getName();
} catch (NoSuchEntityException $noSuchEntityException) {
Expand All @@ -89,6 +91,20 @@ public function mapByProduct(ProductInterface $product): array
return $productData;
}

/**
* @param ProductInterface $product
* @return CategoryInterface
*/
private function getProductCategory($product)
{
if ($this->config->getProductListValueOnCategory() == ProductListValue::CURRENT_CATEGORY
&& $product->getCategory()
) {
return $product->getCategory();
}
return $this->categoryProvider->getFirstByProduct($product);
}

/**
* @return string[]
*/
Expand Down
28 changes: 28 additions & 0 deletions Model/Config/Source/ProductListValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Yireo\GoogleTagManager2\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class ProductListValue implements OptionSourceInterface
{
public const PRODUCT_FIRST_CATEGORY = 'product_first_category';
public const CURRENT_CATEGORY = 'current_category';

/**
* {@inheritdoc}
*/
public function toOptionArray(): array
{
return [
[
'value' => self::PRODUCT_FIRST_CATEGORY,
'label' => __('Product First Category'),
],
[
'value' => self::CURRENT_CATEGORY,
'label' => __('Current Category'),
],
];
}
}
7 changes: 7 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="product_list_value_on_category" type="select" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product List Value on Category Page</label>
<source_model>Yireo\GoogleTagManager2\Model\Config\Source\ProductListValue</source_model>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="product_eav_attributes" type="multiselect" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product EAV Attributes</label>
<source_model>Yireo\GoogleTagManager2\Model\Config\Source\ProductAttributes</source_model>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<customer_eav_attributes>id</customer_eav_attributes>
<view_cart_occurances>everywhere</view_cart_occurances>
<view_cart_on_mini_cart_expand_only>0</view_cart_on_mini_cart_expand_only>
<product_list_value_on_category>product_first_category</product_list_value_on_category>
</settings>
</googletagmanager2>
</default>
Expand Down

0 comments on commit bc67ab2

Please sign in to comment.