forked from magepal/magento2-google-tag-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCart.php
167 lines (143 loc) · 5.02 KB
/
Cart.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\GoogleTagManager\Model;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use MagePal\GoogleTagManager\DataLayer\QuoteData\QuoteItemProvider;
use MagePal\GoogleTagManager\DataLayer\QuoteData\QuoteProvider;
use MagePal\GoogleTagManager\Helper\DataLayerItem as dataLayerItemHelper;
class Cart extends DataObject
{
/**
* @var CheckoutSession
*/
protected $checkoutSession;
/**
* @var dataLayerItemHelper
*/
protected $dataLayerItemHelper;
/**
* Escaper
*
* @var Escaper
*/
protected $_escaper;
/**
* @var QuoteProvider
*/
protected $quoteProvider;
/**
* @var QuoteItemProvider
*/
protected $quoteItemProvider;
/**
* Cart constructor.
* @param CheckoutSession $checkoutSession
* @param dataLayerItemHelper $dataLayerItemHelper
* @param Escaper $escaper
* @param QuoteProvider $quoteProvider
* @param QuoteItemProvider $quoteItemProvider
* @param array $data
*/
public function __construct(
CheckoutSession $checkoutSession,
dataLayerItemHelper $dataLayerItemHelper,
Escaper $escaper,
QuoteProvider $quoteProvider,
QuoteItemProvider $quoteItemProvider,
array $data = []
) {
$this->checkoutSession = $checkoutSession;
$this->dataLayerItemHelper = $dataLayerItemHelper;
$this->_escaper = $escaper;
$this->quoteProvider = $quoteProvider;
$this->quoteItemProvider = $quoteItemProvider;
parent::__construct($data);
}
/**
* Get cart array
*
* @return array
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getCart()
{
$quote = $this->getQuote();
$cart = [];
$cart['hasItems'] = false;
if ($quote->getItemsCount()) {
$items = [];
foreach ($quote->getAllVisibleItems() as $item) {
$itemData = [
'sku' => $item->getSku(),
'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(),
'name' => $item->getName(),
'product_type' => $item->getProductType(),
'price' => $this->dataLayerItemHelper->formatPrice($item->getPrice()),
'price_incl_tax' => $this->dataLayerItemHelper->formatPrice($item->getPriceInclTax()),
'discount_amount' => $this->dataLayerItemHelper->formatPrice($item->getDiscountAmount()),
'tax_amount' => $this->dataLayerItemHelper->formatPrice($item->getTaxAmount()),
'quantity' => $item->getQty() * 1,
];
if ($variant = $this->dataLayerItemHelper->getItemVariant($item)) {
$itemData['variant'] = $variant;
}
if (!empty($category = $this->dataLayerItemHelper->getCategories($item))) {
$itemData['categories'] = $category;
$itemData['category'] = $this->dataLayerItemHelper->getFirstCategory($item);
}
$items[] = $this->quoteItemProvider
->setItem($item)
->setItemData($itemData)
->setActionType(QuoteItemProvider::ACTION_VIEW_CART)
->setListType(QuoteItemProvider::LIST_TYPE_GENERIC)
->getData();
}
if (count($items) > 0) {
$cart['hasItems'] = true;
$cart['items'] = $items;
}
$cart['total'] = $this->dataLayerItemHelper->formatPrice($quote->getGrandTotal());
$cart['itemCount'] = $quote->getItemsCount() * 1;
$cart['cartQty'] = $quote->getItemsQty() * 1;
//set coupon code
$coupon = $quote->getCouponCode();
$cart['hasCoupons'] = $coupon ? true : false;
if ($coupon) {
$cart['couponCode'] = $coupon;
}
}
return $this->quoteProvider->setQuote($this->getQuote())->setTransactionData($cart)->getData();
}
/**
* Get active quote
*
* @return Quote
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getQuote()
{
return $this->checkoutSession->getQuote();
}
/**
* Escape quotes in java scripts
*
* @param string|array $data
* @param string $quote
* @return string|array
*/
public function escapeJsQuote($data, $quote = '\'')
{
return $this->_escaper->escapeJsQuote($data, $quote);
}
}