7
7
8
8
namespace Magento \GoogleGtag \Block ;
9
9
10
- use Magento \Catalog \Api \ProductRepositoryInterface ;
11
10
use Magento \Cookie \Helper \Cookie ;
12
11
use Magento \Framework \Api \SearchCriteriaBuilder ;
13
12
use Magento \Framework \Serialize \SerializerInterface ;
@@ -26,32 +25,27 @@ class Ga extends Template
26
25
/**
27
26
* @var GtagConfiguration
28
27
*/
29
- private GtagConfiguration $ googleGtagConfig ;
28
+ private $ googleGtagConfig ;
30
29
31
30
/**
32
31
* @var Cookie
33
32
*/
34
- private Cookie $ cookieHelper ;
33
+ private $ cookieHelper ;
35
34
36
35
/**
37
36
* @var SerializerInterface
38
37
*/
39
- private SerializerInterface $ serializer ;
38
+ private $ serializer ;
40
39
41
40
/**
42
41
* @var OrderRepositoryInterface
43
42
*/
44
- private OrderRepositoryInterface $ orderRepository ;
43
+ private $ orderRepository ;
45
44
46
45
/**
47
46
* @var SearchCriteriaBuilder
48
47
*/
49
- private SearchCriteriaBuilder $ searchCriteriaBuilder ;
50
-
51
- /**
52
- * @var \Magento\Catalog\Api\ProductRepositoryInterface
53
- */
54
- private ProductRepositoryInterface $ productRepository ;
48
+ private $ searchCriteriaBuilder ;
55
49
56
50
/**
57
51
* @param Context $context
@@ -60,7 +54,6 @@ class Ga extends Template
60
54
* @param SerializerInterface $serializer
61
55
* @param SearchCriteriaBuilder $searchCriteriaBuilder
62
56
* @param OrderRepositoryInterface $orderRepository
63
- * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
64
57
* @param array $data
65
58
*/
66
59
public function __construct (
@@ -70,16 +63,13 @@ public function __construct(
70
63
SerializerInterface $ serializer ,
71
64
SearchCriteriaBuilder $ searchCriteriaBuilder ,
72
65
OrderRepositoryInterface $ orderRepository ,
73
- ProductRepositoryInterface $ productRepository ,
74
66
array $ data = []
75
67
) {
76
68
$ this ->googleGtagConfig = $ googleGtagConfig ;
77
69
$ this ->cookieHelper = $ cookieHelper ;
78
70
$ this ->serializer = $ serializer ;
79
71
$ this ->orderRepository = $ orderRepository ;
80
72
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
81
- $ this ->productRepository = $ productRepository ;
82
-
83
73
parent ::__construct ($ context , $ data );
84
74
}
85
75
@@ -107,7 +97,6 @@ public function isCookieRestrictionModeEnabled(): bool
107
97
* Return current website id.
108
98
*
109
99
* @return int
110
- * @throws \Magento\Framework\Exception\LocalizedException
111
100
*/
112
101
public function getCurrentWebsiteId (): int
113
102
{
@@ -140,7 +129,6 @@ public function getPageTrackingData($measurementId): array
140
129
* @link https://developers.google.com/gtagjs/reference/event#purchase
141
130
*
142
131
* @return array
143
- * @throws \Magento\Framework\Exception\NoSuchEntityException
144
132
*/
145
133
public function getOrdersTrackingData (): array
146
134
{
@@ -157,48 +145,25 @@ public function getOrdersTrackingData(): array
157
145
$ collection = $ this ->orderRepository ->getList ($ this ->searchCriteriaBuilder ->create ());
158
146
159
147
foreach ($ collection ->getItems () as $ order ) {
160
- foreach ($ order ->getAllVisibleItems () as $ index => $ item ) {
161
- $ product = $ this ->productRepository ->get ($ item ->getSku ());
162
- $ orderProduct = [
163
- 'index ' => $ index +1 ,
148
+ foreach ($ order ->getAllVisibleItems () as $ item ) {
149
+ $ result ['products ' ][] = [
164
150
'item_id ' => $ this ->_escaper ->escapeHtmlAttr ($ item ->getSku ()),
165
151
'item_name ' => $ this ->_escaper ->escapeHtmlAttr ($ item ->getName ()),
166
- 'item_brand ' => $ this ->_escaper ->escapeHtmlAttr (
167
- $ product ->getAttributeText ('manufacturer ' )
168
- ),
169
152
'affiliation ' => $ this ->_escaper ->escapeHtmlAttr (
170
153
$ this ->_storeManager ->getStore ()->getFrontendName ()
171
154
),
172
155
'price ' => round ((float ) $ item ->getPrice (), 2 ),
173
156
'quantity ' => (int )$ item ->getQtyOrdered ()
174
157
];
175
-
176
- if ($ item ->getDiscountAmount () > 0 ) {
177
- $ orderProduct ['discount ' ] = $ this ->_escaper ->escapeHtmlAttr ($ item ->getDiscountAmount ());
178
-
179
- if (!empty ($ order ->getCouponCode ())) {
180
- $ orderProduct ['coupon ' ] = $ this ->_escaper ->escapeHtmlAttr ($ order ->getCouponCode ());
181
- }
182
- }
183
-
184
- $ result ['products ' ][] = $ orderProduct ;
185
158
}
186
-
187
- $ resultOrder = [
159
+ $ result ['orders ' ][] = [
188
160
'transaction_id ' => $ order ->getIncrementId (),
189
161
'currency ' => $ order ->getOrderCurrencyCode (),
190
162
'value ' => round ((float ) $ order ->getGrandTotal (), 2 ),
191
163
'tax ' => round ((float ) $ order ->getTaxAmount (), 2 ),
192
164
'shipping ' => round ((float ) $ order ->getShippingAmount (), 2 ),
193
165
];
194
-
195
- if (!empty ($ order ->getCouponCode ())) {
196
- $ resultOrder ['coupon ' ] = $ this ->_escaper ->escapeHtmlAttr ($ order ->getCouponCode ());
197
- }
198
-
199
- $ result ['orders ' ][] = $ resultOrder ;
200
166
}
201
-
202
167
return $ result ;
203
168
}
204
169
@@ -221,7 +186,6 @@ private function getOptPageUrl(): string
221
186
* Provide analytics events data
222
187
*
223
188
* @return bool|string
224
- * @throws \Magento\Framework\Exception\NoSuchEntityException|\Magento\Framework\Exception\LocalizedException
225
189
*/
226
190
public function getAnalyticsData ()
227
191
{
@@ -233,7 +197,6 @@ public function getAnalyticsData()
233
197
'ordersTrackingData ' => $ this ->getOrdersTrackingData (),
234
198
'googleAnalyticsAvailable ' => $ this ->googleGtagConfig ->isGoogleAnalyticsAvailable ()
235
199
];
236
-
237
200
return $ this ->serializer ->serialize ($ analyticData );
238
201
}
239
202
}
0 commit comments