diff --git a/Config/Config.php b/Config/Config.php index 10a5feb7..02874bfa 100644 --- a/Config/Config.php +++ b/Config/Config.php @@ -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 * diff --git a/DataLayer/Mapper/ProductDataMapper.php b/DataLayer/Mapper/ProductDataMapper.php index 496ea95b..68587051 100644 --- a/DataLayer/Mapper/ProductDataMapper.php +++ b/DataLayer/Mapper/ProductDataMapper.php @@ -2,6 +2,7 @@ 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; @@ -9,6 +10,7 @@ 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; @@ -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) { @@ -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[] */ diff --git a/Model/Config/Source/ProductListValue.php b/Model/Config/Source/ProductListValue.php new file mode 100644 index 00000000..698f10ce --- /dev/null +++ b/Model/Config/Source/ProductListValue.php @@ -0,0 +1,28 @@ + self::PRODUCT_FIRST_CATEGORY, + 'label' => __('Product First Category'), + ], + [ + 'value' => self::CURRENT_CATEGORY, + 'label' => __('Current Category'), + ], + ]; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index a2ba31fe..6ed9f744 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -67,6 +67,13 @@ 1 + + + Yireo\GoogleTagManager2\Model\Config\Source\ProductListValue + + 1 + + Yireo\GoogleTagManager2\Model\Config\Source\ProductAttributes diff --git a/etc/config.xml b/etc/config.xml index 1ae1def3..811da262 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -13,6 +13,7 @@ id everywhere 0 + product_first_category