-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewCart.php
95 lines (84 loc) · 2.6 KB
/
ViewCart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Taggrs\DataLayer\Block\Event;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Taggrs\DataLayer\Block\DataLayer;
use Taggrs\DataLayer\Helper\ProductViewDataHelper;
use Taggrs\DataLayer\Helper\QuoteDataHelper;
use Taggrs\DataLayer\Helper\UserDataHelper;
/**
* Generates Data Layer for the view_cart event on the Checkout Cart page
*/
class ViewCart extends DataLayer
{
/**
* @var CheckoutSession to retrieve data from the quote
*/
private CheckoutSession $checkoutSession;
/**
* @var QuoteDataHelper to help retrieve data from the quote
*/
private QuoteDataHelper $quoteDataHelper;
/**
* Class constructor
*
* @param CheckoutSession $checkoutSession
* @param QuoteDataHelper $quoteDataHelper
* @param UserDataHelper $userDataHelper
* @param Context $context
* @param array $data
*/
public function __construct(
CheckoutSession $checkoutSession,
QuoteDataHelper $quoteDataHelper,
UserDataHelper $userDataHelper,
Template\Context $context,
array $data = []
) {
parent::__construct($userDataHelper, $context, $data);
$this->checkoutSession = $checkoutSession;
$this->quoteDataHelper = $quoteDataHelper;
}
/**
* Get the event name
*
* @return string the event name
*/
public function getEvent(): string
{
return 'view_cart';
}
/**
* Get the e-commerce Data Layer
*
* @return array containing the e-commerce data
*/
public function getEcommerce(): array
{
try {
$currency = $this->_storeManager
->getStore()
->getCurrentCurrency()
->getCode()
;
} catch ( NoSuchEntityException|LocalizedException $e ) {
$currency = null;
}
$items = $this->quoteDataHelper->getItemsFromQuote();
try {
$value = (float) $this->checkoutSession->getQuote()->getGrandTotal();
} catch ( NoSuchEntityException|LocalizedException $e ) {
$value = 0;
}
return [
'currency' => $currency,
'value' => $value,
'coupon' => $this->quoteDataHelper->getQuote()->getCouponCode() ?? null,
'items' => $items,
'user_data' => $this->getUserData()
];
}
}