Skip to content

Commit 4f33468

Browse files
committed
Allow dataLayerMapping DI type in all data mappers
1 parent cc172c2 commit 4f33468

10 files changed

+276
-14
lines changed

Api/Data/CartItemTagInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\GoogleTagManager2\Api\Data;
4+
5+
use Magento\Quote\Model\Quote\Item as CartItem;
6+
7+
interface CartItemTagInterface extends TagInterface
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function setCartItem(CartItem $cartItem);
13+
}

Api/Data/CategoryTagInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\GoogleTagManager2\Api\Data;
4+
5+
use Magento\Catalog\Api\Data\CategoryInterface;
6+
7+
interface CategoryTagInterface extends TagInterface
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function setCategory(CategoryInterface $category);
13+
}

Api/Data/CustomerTagInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\GoogleTagManager2\Api\Data;
4+
5+
use Magento\Customer\Api\Data\CustomerInterface;
6+
7+
interface CustomerTagInterface extends TagInterface
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function setCustomer(CustomerInterface $customer);
13+
}

Api/Data/OrderItemTagInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\GoogleTagManager2\Api\Data;
4+
5+
use Magento\Sales\Api\Data\OrderItemInterface;
6+
7+
interface OrderItemTagInterface extends TagInterface
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function setOrderItem(OrderItemInterface $orderItem);
13+
}

Api/Data/OrderTagInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\GoogleTagManager2\Api\Data;
4+
5+
use Magento\Sales\Api\Data\OrderInterface;
6+
7+
interface OrderTagInterface extends TagInterface
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function setOrder(OrderInterface $order);
13+
}

DataLayer/Mapper/CartItemDataMapper.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\Quote\Model\Quote\Item as CartItem;
99
use Magento\Store\Model\ScopeInterface;
1010
use Magento\Tax\Model\Config;
11+
use Yireo\GoogleTagManager2\Api\Data\CartItemTagInterface;
12+
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
1113
use Yireo\GoogleTagManager2\Util\PriceFormatter;
1214
use Yireo\GoogleTagManager2\Util\ProductProvider;
1315

@@ -18,6 +20,8 @@ class CartItemDataMapper
1820
private PriceFormatter $priceFormatter;
1921
private ScopeConfigInterface $scopeConfig;
2022
private bool $useProductProvider = false;
23+
private array $dataLayerMapping;
24+
2125

2226
/**
2327
* @param ProductDataMapper $productDataMapper
@@ -30,13 +34,15 @@ public function __construct(
3034
ProductProvider $productProvider,
3135
PriceFormatter $priceFormatter,
3236
ScopeConfigInterface $scopeConfig,
33-
bool $useProductProvider = false
37+
bool $useProductProvider = false,
38+
array $dataLayerMapping = []
3439
) {
3540
$this->productDataMapper = $productDataMapper;
3641
$this->productProvider = $productProvider;
3742
$this->priceFormatter = $priceFormatter;
3843
$this->scopeConfig = $scopeConfig;
3944
$this->useProductProvider = $useProductProvider;
45+
$this->dataLayerMapping = $dataLayerMapping;
4046
}
4147

4248
/**
@@ -59,7 +65,7 @@ public function mapByCartItem(CartItem $cartItem): array
5965
}
6066

6167
$price = $this->getPrice($cartItem);
62-
return array_merge($cartItemData, [
68+
$cartItemData = array_merge($cartItemData, [
6369
'item_sku' => $cartItem->getSku(),
6470
'item_name' => $cartItem->getName(),
6571
'order_item_id' => $cartItem->getItemId(),
@@ -69,6 +75,9 @@ public function mapByCartItem(CartItem $cartItem): array
6975
'price' => $price,
7076
'discount' => $this->getPriceDiscount($cartItem, $price)
7177
]);
78+
79+
$cartItemData = $this->parseDataLayerMapping($cartItem, $cartItemData);
80+
return $cartItemData;
7281
}
7382

7483
/**
@@ -101,4 +110,36 @@ private function getPriceDiscount(CartItem $cartItem, float $price): float
101110
{
102111
return $cartItem->getDiscountAmount() / $cartItem->getQty();
103112
}
113+
114+
/**
115+
* @param CartItem $cartItem
116+
* @param array $data
117+
*
118+
* @return array
119+
*/
120+
private function parseDataLayerMapping(CartItem $cartItem, array $data): array
121+
{
122+
if (empty($this->dataLayerMapping)) {
123+
return [];
124+
}
125+
126+
foreach ($this->dataLayerMapping as $tagName => $tagValue) {
127+
if (is_string($tagValue) && array_key_exists($tagValue, $data)) {
128+
$data[$tagName] = $data[$tagValue];
129+
continue;
130+
}
131+
132+
if ($tagValue instanceof CartItemTagInterface) {
133+
$tagValue->setCartItem($cartItem);
134+
$data[$tagName] = $tagValue->get();
135+
continue;
136+
}
137+
138+
if ($tagValue instanceof TagInterface) {
139+
$data[$tagName] = $tagValue->get();
140+
}
141+
}
142+
143+
return $data;
144+
}
104145
}

DataLayer/Mapper/CategoryDataMapper.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
use Magento\Catalog\Api\Data\CategoryInterface;
66
use Magento\Framework\Exception\LocalizedException;
7+
use Yireo\GoogleTagManager2\Api\Data\CategoryTagInterface;
8+
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
79
use Yireo\GoogleTagManager2\Config\Config;
810
use Yireo\GoogleTagManager2\Util\Attribute\GetAttributeValue;
911

1012
class CategoryDataMapper
1113
{
1214
private Config $config;
1315
private GetAttributeValue $getAttributeValue;
16+
private array $dataLayerMapping;
1417

1518
/**
1619
* @param Config $config
1720
* @param GetAttributeValue $getAttributeValue
1821
*/
1922
public function __construct(
2023
Config $config,
21-
GetAttributeValue $getAttributeValue
24+
GetAttributeValue $getAttributeValue,
25+
array $dataLayerMapping = []
2226
) {
2327
$this->config = $config;
2428
$this->getAttributeValue = $getAttributeValue;
29+
$this->dataLayerMapping = $dataLayerMapping;
2530
}
2631

2732
/**
@@ -44,6 +49,8 @@ public function mapByCategory(CategoryInterface $category): array
4449
$categoryData[$dataLayerKey] = $attributeValue;
4550
}
4651

52+
$categoryData = $this->parseDataLayerMapping($category, $categoryData);
53+
4754
return $categoryData;
4855
}
4956

@@ -54,4 +61,36 @@ private function getCategoryFields(): array
5461
{
5562
return array_filter(array_merge(['id', 'name'], $this->config->getCategoryEavAttributeCodes()));
5663
}
64+
65+
/**
66+
* @param CategoryInterface $category
67+
* @param array $data
68+
*
69+
* @return array
70+
*/
71+
private function parseDataLayerMapping(CategoryInterface $category, array $data): array
72+
{
73+
if (empty($this->dataLayerMapping)) {
74+
return [];
75+
}
76+
77+
foreach ($this->dataLayerMapping as $tagName => $tagValue) {
78+
if (is_string($tagValue) && array_key_exists($tagValue, $data)) {
79+
$data[$tagName] = $data[$tagValue];
80+
continue;
81+
}
82+
83+
if ($tagValue instanceof CategoryTagInterface) {
84+
$tagValue->setCategory($category);
85+
$data[$tagName] = $tagValue->get();
86+
continue;
87+
}
88+
89+
if ($tagValue instanceof TagInterface) {
90+
$data[$tagName] = $tagValue->get();
91+
}
92+
}
93+
94+
return $data;
95+
}
5796
}

DataLayer/Mapper/CustomerDataMapper.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Yireo\GoogleTagManager2\DataLayer\Mapper;
44

55
use Magento\Customer\Api\Data\CustomerInterface;
6+
use Yireo\GoogleTagManager2\Api\Data\CustomerTagInterface;
7+
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
68
use Yireo\GoogleTagManager2\Config\Config;
79
use Yireo\GoogleTagManager2\Util\Attribute\GetAttributeValue;
810
use Yireo\GoogleTagManager2\Util\CamelCase;
@@ -12,20 +14,24 @@ class CustomerDataMapper
1214
private CamelCase $camelCase;
1315
private Config $config;
1416
private GetAttributeValue $getAttributeValue;
17+
private array $dataLayerMapping;
1518

1619
/**
17-
* @param CamelCase $camelCase
18-
* @param Config $config
20+
* @param CamelCase $camelCase
21+
* @param Config $config
1922
* @param GetAttributeValue $getAttributeValue
23+
* @param array $dataLayerMapping
2024
*/
2125
public function __construct(
2226
CamelCase $camelCase,
2327
Config $config,
24-
GetAttributeValue $getAttributeValue
28+
GetAttributeValue $getAttributeValue,
29+
array $dataLayerMapping = []
2530
) {
2631
$this->camelCase = $camelCase;
2732
$this->config = $config;
2833
$this->getAttributeValue = $getAttributeValue;
34+
$this->dataLayerMapping = $dataLayerMapping;
2935
}
3036

3137
/**
@@ -48,6 +54,8 @@ public function mapByCustomer(CustomerInterface $customer, string $prefix = ''):
4854
$customerData[$dataLayerKey] = $attributeValue;
4955
}
5056

57+
$customerData = $this->parseDataLayerMapping($customer, $customerData);
58+
5159
return $customerData;
5260
}
5361

@@ -58,4 +66,35 @@ private function getCustomerFields(): array
5866
{
5967
return array_filter(array_merge(['id'], $this->config->getCustomerEavAttributeCodes()));
6068
}
69+
70+
/**
71+
* @param CustomerInterface $customer
72+
* @param array $data
73+
* @return array
74+
*/
75+
private function parseDataLayerMapping(CustomerInterface $customer, array $data): array
76+
{
77+
if (empty($this->dataLayerMapping)) {
78+
return [];
79+
}
80+
81+
foreach ($this->dataLayerMapping as $tagName => $tagValue) {
82+
if (is_string($tagValue) && array_key_exists($tagValue, $data)) {
83+
$data[$tagName] = $data[$tagValue];
84+
continue;
85+
}
86+
87+
if ($tagValue instanceof CustomerTagInterface) {
88+
$tagValue->setCustomer($customer);
89+
$data[$tagName] = $tagValue->get();
90+
continue;
91+
}
92+
93+
if ($tagValue instanceof TagInterface) {
94+
$data[$tagName] = $tagValue->get();
95+
}
96+
}
97+
98+
return $data;
99+
}
61100
}

0 commit comments

Comments
 (0)