Skip to content

Commit 21be2a8

Browse files
committed
Missing value & currency in remove_from_cart event #285
1 parent a68f154 commit 21be2a8

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

DataLayer/Event/AddToCart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AddToCart implements EventInterface
1515
private ProductDataMapper $productDataMapper;
1616
private CurrencyCode $currencyCode;
1717
private PriceFormatter $priceFormatter;
18-
private Product $product;
18+
private ?Product $product = null;
1919
private int $qty = 1;
2020

2121
/**

DataLayer/Event/RemoveFromCart.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,50 @@
55
use Magento\Quote\Api\Data\CartItemInterface;
66
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
77
use Yireo\GoogleTagManager2\DataLayer\Mapper\CartItemDataMapper;
8+
use Yireo\GoogleTagManager2\DataLayer\Tag\CurrencyCode;
9+
use Yireo\GoogleTagManager2\Util\PriceFormatter;
810

911
class RemoveFromCart implements EventInterface
1012
{
13+
private ?CartItemInterface $cartItem = null;
1114
private CartItemDataMapper $cartItemDataMapper;
12-
private CartItemInterface $cartItem;
15+
private CurrencyCode $currencyCode;
16+
private PriceFormatter $priceFormatter;
1317

1418
/**
1519
* @param CartItemDataMapper $cartItemDataMapper
1620
*/
17-
public function __construct(CartItemDataMapper $cartItemDataMapper)
18-
{
21+
public function __construct(
22+
CartItemDataMapper $cartItemDataMapper,
23+
CurrencyCode $currencyCode,
24+
PriceFormatter $priceFormatter
25+
) {
1926
$this->cartItemDataMapper = $cartItemDataMapper;
27+
$this->currencyCode = $currencyCode;
28+
$this->priceFormatter = $priceFormatter;
2029
}
2130

2231
/**
2332
* @return array
2433
*/
2534
public function get(): array
2635
{
27-
$cartItemData = $this->cartItemDataMapper->mapByCartItem($this->cartItem);
36+
$itemData = $this->cartItemDataMapper->mapByCartItem($this->cartItem);
37+
$value = $itemData['price'] * $itemData['quantity'];
38+
2839
return [
2940
'event' => 'remove_from_cart',
3041
'ecommerce' => [
31-
'items' => [$cartItemData]
42+
'currency' => $this->currencyCode->get(),
43+
'value' => $this->priceFormatter->format((float)$value),
44+
'items' => [$itemData]
3245
]
3346
];
3447
}
3548

3649
/**
3750
* @param CartItemInterface $cartItem
51+
*
3852
* @return RemoveFromCart
3953
*/
4054
public function setCartItem(CartItemInterface $cartItem): RemoveFromCart

0 commit comments

Comments
 (0)