8
8
namespace Magento \GoogleGtag \Block ;
9
9
10
10
use Magento \Cookie \Helper \Cookie ;
11
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
12
+ use Magento \Framework \Serialize \SerializerInterface ;
11
13
use Magento \Framework \View \Element \Template ;
12
14
use Magento \Framework \View \Element \Template \Context ;
13
- use Magento \GoogleGtag \Helper \GtagConfiguration ;
14
- use Magento \Sales \Model \ResourceModel \Order \CollectionFactory ;
15
- use Magento \Store \Model \ScopeInterface ;
15
+ use Magento \GoogleGtag \Model \Config \GtagConfig as GtagConfiguration ;
16
+ use Magento \Sales \Api \OrderRepositoryInterface ;
16
17
17
18
/**
18
19
* GoogleAnalytics Page Block
@@ -27,56 +28,51 @@ class Ga extends Template
27
28
private $ googleGtagConfig ;
28
29
29
30
/**
30
- * @var CollectionFactory
31
+ * @var Cookie
31
32
*/
32
- private $ salesOrderCollection ;
33
+ private $ cookieHelper ;
33
34
34
35
/**
35
- * @var Cookie
36
+ * @var SerializerInterface
36
37
*/
37
- private $ cookieHelper ;
38
+ private $ serializer ;
39
+
40
+ /**
41
+ * @var OrderRepositoryInterface
42
+ */
43
+ private $ orderRepository ;
44
+
45
+ /**
46
+ * @var SearchCriteriaBuilder
47
+ */
48
+ private $ searchCriteriaBuilder ;
38
49
39
50
/**
40
51
* @param Context $context
41
- * @param CollectionFactory $salesOrderCollection
42
52
* @param GtagConfiguration $googleGtagConfig
43
- * @param array $data
44
53
* @param Cookie $cookieHelper
54
+ * @param SerializerInterface $serializer
55
+ * @param SearchCriteriaBuilder $searchCriteriaBuilder
56
+ * @param OrderRepositoryInterface $orderRepository
57
+ * @param array $data
45
58
*/
46
59
public function __construct (
47
60
Context $ context ,
48
- CollectionFactory $ salesOrderCollection ,
49
61
GtagConfiguration $ googleGtagConfig ,
50
62
Cookie $ cookieHelper ,
63
+ SerializerInterface $ serializer ,
64
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
65
+ OrderRepositoryInterface $ orderRepository ,
51
66
array $ data = []
52
67
) {
53
68
$ this ->googleGtagConfig = $ googleGtagConfig ;
54
- $ this ->salesOrderCollection = $ salesOrderCollection ;
55
69
$ this ->cookieHelper = $ cookieHelper ;
70
+ $ this ->serializer = $ serializer ;
71
+ $ this ->orderRepository = $ orderRepository ;
72
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
56
73
parent ::__construct ($ context , $ data );
57
74
}
58
75
59
- /**
60
- * Get config
61
- *
62
- * @param string $path
63
- * @return mixed
64
- */
65
- public function getConfig ($ path ): string
66
- {
67
- return $ this ->_scopeConfig ->getValue ($ path , ScopeInterface::SCOPE_STORE );
68
- }
69
-
70
- /**
71
- * Get helper
72
- *
73
- * @return GtagConfiguration
74
- */
75
- public function getHelper (): GtagConfiguration
76
- {
77
- return $ this ->googleGtagConfig ;
78
- }
79
-
80
76
/**
81
77
* Get a specific page name (may be customized via layout)
82
78
*
@@ -147,7 +143,6 @@ public function getPageTrackingData($measurementId): array
147
143
* @link https://developers.google.com/gtagjs/reference/event#purchase
148
144
*
149
145
* @return array
150
- * @since 100.2.0
151
146
*/
152
147
public function getOrdersTrackingData (): array
153
148
{
@@ -156,25 +151,28 @@ public function getOrdersTrackingData(): array
156
151
if (empty ($ orderIds ) || !is_array ($ orderIds )) {
157
152
return $ result ;
158
153
}
159
-
160
- $ collection = $ this ->salesOrderCollection ->create ();
161
- $ collection ->addFieldToFilter ('entity_id ' , ['in ' => $ orderIds ]);
162
-
163
- foreach ($ collection as $ order ) {
154
+ $ this ->searchCriteriaBuilder ->addFilter (
155
+ 'entity_id ' ,
156
+ $ orderIds ,
157
+ 'in '
158
+ );
159
+ $ collection = $ this ->orderRepository ->getList ($ this ->searchCriteriaBuilder ->create ());
160
+
161
+ foreach ($ collection ->getItems () as $ order ) {
164
162
foreach ($ order ->getAllVisibleItems () as $ item ) {
165
163
$ result ['products ' ][] = [
166
164
'item_id ' => $ this ->escapeJsQuote ($ item ->getSku ()),
167
165
'item_name ' => $ this ->escapeJsQuote ($ item ->getName ()),
168
- 'price ' => $ this -> googleGtagConfig -> formatToDec ((float ) $ item ->getPrice ()),
166
+ 'price ' => number_format ((float ) $ item ->getPrice (), 2 ),
169
167
'quantity ' => (int )$ item ->getQtyOrdered (),
170
168
];
171
169
}
172
170
$ result ['orders ' ][] = [
173
171
'transaction_id ' => $ order ->getIncrementId (),
174
172
'affiliation ' => $ this ->escapeJsQuote ($ this ->_storeManager ->getStore ()->getFrontendName ()),
175
- 'value ' => $ this -> googleGtagConfig -> formatToDec ((float ) $ order ->getGrandTotal ()),
176
- 'tax ' => $ this -> googleGtagConfig -> formatToDec ((float ) $ order ->getTaxAmount ()),
177
- 'shipping ' => $ this -> googleGtagConfig -> formatToDec ((float ) $ order ->getShippingAmount ()),
173
+ 'value ' => number_format ((float ) $ order ->getGrandTotal (), 2 ),
174
+ 'tax ' => number_format ((float ) $ order ->getTaxAmount (), 2 ),
175
+ 'shipping ' => number_format ((float ) $ order ->getShippingAmount (), 2 ),
178
176
];
179
177
$ result ['currency ' ] = $ order ->getOrderCurrencyCode ();
180
178
}
@@ -195,4 +193,21 @@ private function getOptPageUrl(): string
195
193
}
196
194
return $ optPageURL ;
197
195
}
196
+
197
+ /**
198
+ * Provide analytics events data
199
+ *
200
+ * @return bool|string
201
+ */
202
+ public function getAnalyticsData ()
203
+ {
204
+ $ analyticData = [
205
+ 'isCookieRestrictionModeEnabled ' => $ this ->isCookieRestrictionModeEnabled (),
206
+ 'currentWebsite ' => $ this ->getCurrentWebsiteId (),
207
+ 'cookieName ' => Cookie::IS_USER_ALLOWED_SAVE_COOKIE ,
208
+ 'pageTrackingData ' => $ this ->getPageTrackingData ($ this ->googleGtagConfig ->getMeasurementId ()),
209
+ 'ordersTrackingData ' => $ this ->getOrdersTrackingData ()
210
+ ];
211
+ return $ this ->serializer ->serialize ($ analyticData );
212
+ }
198
213
}
0 commit comments