@@ -131,67 +131,57 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
131
131
$ request = $ this ->getRequest ();
132
132
$ moduleName = $ request ->getModuleName ();
133
133
$ controllerName = $ request ->getControllerName ();
134
+ $ helper = Mage::helper ('googleanalytics ' );
134
135
135
136
/**
136
137
* This event signifies that an item was removed from a cart.
137
138
*
138
139
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#remove_from_cart
139
140
*/
140
- $ removedProducts = Mage::getSingleton ('core/session ' )->getRemovedProductsCart ();
141
+ $ removedProducts = Mage::getSingleton ('core/session ' )->getRemovedProductsForAnalytics ();
141
142
if ($ removedProducts ) {
142
143
foreach ($ removedProducts as $ removedProduct ) {
143
- $ _removedProduct = Mage::getModel ('catalog/product ' )->load ($ removedProduct );
144
144
$ eventData = [];
145
145
$ eventData ['currency ' ] = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
146
- $ eventData ['value ' ] = number_format ( $ _removedProduct -> getFinalPrice (), 2 , ' . ' , '' );
146
+ $ eventData ['value ' ] = $ helper -> formatPrice ( $ removedProduct [ ' price ' ] * $ removedProduct [ ' qty ' ] );
147
147
$ eventData ['items ' ] = [];
148
148
$ _item = [
149
- 'item_id ' => $ _removedProduct ->getSku (),
150
- 'item_name ' => $ _removedProduct ->getName (),
151
- 'price ' => number_format ($ _removedProduct ->getFinalPrice (), 2 , '. ' , '' ),
149
+ 'item_id ' => $ removedProduct ['sku ' ],
150
+ 'item_name ' => $ removedProduct ['name ' ],
151
+ 'price ' => $ helper ->formatPrice ($ removedProduct ['price ' ]),
152
+ 'quantity ' => (int ) $ removedProduct ['qty ' ],
153
+ 'item_brand ' => $ removedProduct ['manufacturer ' ],
154
+ 'item_category ' => $ removedProduct ['category ' ],
152
155
];
153
- if ($ _removedProduct ->getAttributeText ('manufacturer ' )) {
154
- $ _item ['item_brand ' ] = $ _removedProduct ->getAttributeText ('manufacturer ' );
155
- }
156
- $ itemCategory = Mage::helper ('googleanalytics ' )->getLastCategoryName ($ _removedProduct );
157
- if ($ itemCategory ) {
158
- $ _item ['item_category ' ] = $ itemCategory ;
159
- }
160
- array_push ($ eventData ['items ' ], $ _item );
156
+ $ eventData ['items ' ][] = $ _item ;
161
157
$ result [] = "gtag('event', 'remove_from_cart', " . json_encode ($ eventData , JSON_THROW_ON_ERROR ) . "); " ;
162
158
}
163
- Mage::getSingleton ('core/session ' )->unsRemovedProductsCart ();
159
+ Mage::getSingleton ('core/session ' )->unsRemovedProductsForAnalytics ();
164
160
}
165
161
166
162
/**
167
163
* This event signifies that an item was added to a cart for purchase.
168
164
*
169
165
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#add_to_cart
170
166
*/
171
- $ addedProducts = Mage::getSingleton ('core/session ' )->getAddedProductsCart ();
167
+ $ addedProducts = Mage::getSingleton ('core/session ' )->getAddedProductsForAnalytics ();
172
168
if ($ addedProducts ) {
173
- foreach ($ addedProducts as $ addedProduct ) {
174
- $ _addedProduct = Mage::getModel ('catalog/product ' )->load ($ addedProduct );
169
+ foreach ($ addedProducts as $ _addedProduct ) {
175
170
$ eventData = [];
176
171
$ eventData ['currency ' ] = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
177
- $ eventData ['value ' ] = number_format ( $ _addedProduct -> getFinalPrice (), 2 , ' . ' , '' );
172
+ $ eventData ['value ' ] = $ helper -> formatPrice ( $ _addedProduct [ ' price ' ] * $ _addedProduct [ ' qty ' ] );
178
173
$ eventData ['items ' ] = [];
179
174
$ _item = [
180
- 'item_id ' => $ _addedProduct ->getSku (),
181
- 'item_name ' => $ _addedProduct ->getName (),
182
- 'price ' => number_format ($ _addedProduct ->getFinalPrice (), 2 , '. ' , '' ),
175
+ 'item_id ' => $ _addedProduct ['sku ' ],
176
+ 'item_name ' => $ _addedProduct ['name ' ],
177
+ 'price ' => $ helper ->formatPrice ($ _addedProduct ['price ' ]),
178
+ 'quantity ' => (int ) $ _addedProduct ['qty ' ],
179
+ 'item_brand ' => $ _addedProduct ['manufacturer ' ],
180
+ 'item_category ' => $ _addedProduct ['category ' ],
183
181
];
184
- if ($ _addedProduct ->getAttributeText ('manufacturer ' )) {
185
- $ _item ['item_brand ' ] = $ _addedProduct ->getAttributeText ('manufacturer ' );
186
- }
187
-
188
- $ itemCategory = Mage::helper ('googleanalytics ' )->getLastCategoryName ($ _addedProduct );
189
- if ($ itemCategory ) {
190
- $ _item ['item_category ' ] = $ itemCategory ;
191
- }
192
- array_push ($ eventData ['items ' ], $ _item );
182
+ $ eventData ['items ' ][] = $ _item ;
193
183
$ result [] = "gtag('event', 'add_to_cart', " . json_encode ($ eventData , JSON_THROW_ON_ERROR ) . "); " ;
194
- Mage::getSingleton ('core/session ' )->unsAddedProductsCart ();
184
+ Mage::getSingleton ('core/session ' )->unsAddedProductsForAnalytics ();
195
185
}
196
186
}
197
187
@@ -205,14 +195,14 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
205
195
$ category = Mage::registry ('current_category ' ) ? Mage::registry ('current_category ' )->getName () : false ;
206
196
$ eventData = [];
207
197
$ eventData ['currency ' ] = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
208
- $ eventData ['value ' ] = number_format ($ productViewed ->getFinalPrice (), 2 , ' . ' , '' );
198
+ $ eventData ['value ' ] = $ helper -> formatPrice ($ productViewed ->getFinalPrice ());
209
199
$ eventData ['items ' ] = [];
210
200
$ _item = [
211
201
'item_id ' => $ productViewed ->getSku (),
212
202
'item_name ' => $ productViewed ->getName (),
213
203
'list_name ' => 'Product Detail Page ' ,
214
204
'item_category ' => $ category ,
215
- 'price ' => number_format ($ productViewed ->getFinalPrice (), 2 , ' . ' , '' ),
205
+ 'price ' => $ helper -> formatPrice ($ productViewed ->getFinalPrice ()),
216
206
];
217
207
if ($ productViewed ->getAttributeText ('manufacturer ' )) {
218
208
$ _item ['item_brand ' ] = $ productViewed ->getAttributeText ('manufacturer ' );
@@ -251,7 +241,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
251
241
'item_id ' => $ productViewed ->getSku (),
252
242
'index ' => $ index ,
253
243
'item_name ' => $ productViewed ->getName (),
254
- 'price ' => number_format ($ productViewed ->getFinalPrice (), 2 , ' . ' , '' ),
244
+ 'price ' => $ helper -> formatPrice ($ productViewed ->getFinalPrice ()),
255
245
];
256
246
if ($ productViewed ->getAttributeText ('manufacturer ' )) {
257
247
$ _item ['item_brand ' ] = $ productViewed ->getAttributeText ('manufacturer ' );
@@ -263,7 +253,7 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
263
253
$ index ++;
264
254
$ eventData ['value ' ] += $ productViewed ->getFinalPrice ();
265
255
}
266
- $ eventData ['value ' ] = number_format ($ eventData ['value ' ], 2 , ' . ' , '' );
256
+ $ eventData ['value ' ] = $ helper -> formatPrice ($ eventData ['value ' ]);
267
257
$ result [] = "gtag('event', 'view_item_list', " . json_encode ($ eventData , JSON_THROW_ON_ERROR ) . "); " ;
268
258
}
269
259
@@ -273,32 +263,34 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
273
263
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart
274
264
*/
275
265
elseif ($ moduleName == 'checkout ' && $ controllerName == 'cart ' ) {
276
- $ productCollection = Mage::getSingleton ('checkout/session ' )->getQuote ()->getAllVisibleItems ();
266
+ $ productCollection = Mage::getSingleton ('checkout/session ' )->getQuote ()->getAllItems ();
277
267
$ eventData = [];
278
268
$ eventData ['currency ' ] = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
279
269
$ eventData ['value ' ] = 0.00 ;
280
270
$ eventData ['items ' ] = [];
281
271
282
272
foreach ($ productCollection as $ productInCart ) {
283
- $ _product = Mage::getModel ('catalog/product ' )->load ($ productInCart ->getProductId ());
273
+ if ($ productInCart ->getParentItem ()) {
274
+ continue ;
275
+ }
276
+ $ _product = $ productInCart ->getProduct ();
284
277
$ _item = [
285
278
'item_id ' => $ _product ->getSku (),
286
279
'item_name ' => $ _product ->getName (),
287
- 'price ' => number_format ($ _product ->getFinalPrice (), 2 , ' . ' , '' ),
280
+ 'price ' => $ helper -> formatPrice ($ _product ->getFinalPrice ()),
288
281
'quantity ' => (int ) $ productInCart ->getQty (),
289
282
];
290
283
if ($ _product ->getAttributeText ('manufacturer ' )) {
291
284
$ _item ['item_brand ' ] = $ _product ->getAttributeText ('manufacturer ' );
292
285
}
293
-
294
- $ itemCategory = Mage::helper ('googleanalytics ' )->getLastCategoryName ($ _product );
286
+ $ itemCategory = $ helper ->getLastCategoryName ($ _product );
295
287
if ($ itemCategory ) {
296
288
$ _item ['item_category ' ] = $ itemCategory ;
297
289
}
298
290
array_push ($ eventData ['items ' ], $ _item );
299
- $ eventData ['value ' ] += $ _product ->getFinalPrice ();
291
+ $ eventData ['value ' ] += $ _product ->getFinalPrice () * $ productInCart -> getQty () ;
300
292
}
301
- $ eventData ['value ' ] = number_format ($ eventData ['value ' ], 2 , ' . ' , '' );
293
+ $ eventData ['value ' ] = $ helper -> formatPrice ($ eventData ['value ' ]);
302
294
$ result [] = "gtag('event', 'view_cart', " . json_encode ($ eventData , JSON_THROW_ON_ERROR ) . "); " ;
303
295
}
304
296
@@ -308,32 +300,34 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
308
300
* @link https://developers.google.com/tag-platform/gtagjs/reference/events#begin_checkout
309
301
*/
310
302
elseif ($ moduleName == static ::CHECKOUT_MODULE_NAME && $ controllerName == static ::CHECKOUT_CONTROLLER_NAME ) {
311
- $ productCollection = Mage::getSingleton ('checkout/session ' )->getQuote ()->getAllVisibleItems ();
303
+ $ productCollection = Mage::getSingleton ('checkout/session ' )->getQuote ()->getAllItems ();
312
304
if ($ productCollection ) {
313
305
$ eventData = [];
314
306
$ eventData ['currency ' ] = Mage::app ()->getStore ()->getCurrentCurrencyCode ();
315
307
$ eventData ['value ' ] = 0.00 ;
316
308
$ eventData ['items ' ] = [];
317
309
foreach ($ productCollection as $ productInCart ) {
318
- $ _product = Mage::getModel ('catalog/product ' )->load ($ productInCart ->getProductId ());
310
+ if ($ productInCart ->getParentItem ()) {
311
+ continue ;
312
+ }
313
+ $ _product = $ productInCart ->getProduct ();
319
314
$ _item = [
320
315
'item_id ' => $ _product ->getSku (),
321
316
'item_name ' => $ _product ->getName (),
322
- 'price ' => number_format ($ _product ->getFinalPrice (), 2 , ' . ' , '' ),
317
+ 'price ' => $ helper -> formatPrice ($ _product ->getFinalPrice ()),
323
318
'quantity ' => (int ) $ productInCart ->getQty (),
324
319
];
325
320
if ($ _product ->getAttributeText ('manufacturer ' )) {
326
321
$ _item ['item_brand ' ] = $ _product ->getAttributeText ('manufacturer ' );
327
322
}
328
-
329
- $ itemCategory = Mage::helper ('googleanalytics ' )->getLastCategoryName ($ _product );
323
+ $ itemCategory = $ helper ->getLastCategoryName ($ _product );
330
324
if ($ itemCategory ) {
331
325
$ _item ['item_category ' ] = $ itemCategory ;
332
326
}
333
327
array_push ($ eventData ['items ' ], $ _item );
334
328
$ eventData ['value ' ] += $ _product ->getFinalPrice ();
335
329
}
336
- $ eventData ['value ' ] = number_format ($ eventData ['value ' ], 2 , ' . ' , '' );
330
+ $ eventData ['value ' ] = $ helper -> formatPrice ($ eventData ['value ' ]);
337
331
$ result [] = "gtag('event', 'begin_checkout', " . json_encode ($ eventData , JSON_THROW_ON_ERROR ) . "); " ;
338
332
}
339
333
}
@@ -352,28 +346,30 @@ protected function _getEnhancedEcommerceDataForAnalytics4()
352
346
$ orderData = [
353
347
'currency ' => $ order ->getBaseCurrencyCode (),
354
348
'transaction_id ' => $ order ->getIncrementId (),
355
- 'value ' => number_format ($ order ->getBaseGrandTotal (), 2 , ' . ' , '' ),
356
- 'coupon ' => strtoupper ($ order ->getCouponCode ()),
357
- 'shipping ' => number_format ($ order ->getBaseShippingAmount (), 2 , ' . ' , '' ),
358
- 'tax ' => number_format ($ order ->getBaseTaxAmount (), 2 , ' . ' , '' ),
349
+ 'value ' => $ helper -> formatPrice ($ order ->getBaseGrandTotal ()),
350
+ 'coupon ' => strtoupper (( string ) $ order ->getCouponCode ()),
351
+ 'shipping ' => $ helper -> formatPrice ($ order ->getBaseShippingAmount ()),
352
+ 'tax ' => $ helper -> formatPrice ($ order ->getBaseTaxAmount ()),
359
353
'items ' => []
360
354
];
361
355
362
356
/** @var Mage_Sales_Model_Order_Item $item */
363
- foreach ($ order ->getAllVisibleItems () as $ item ) {
357
+ foreach ($ order ->getAllItems () as $ item ) {
358
+ if ($ item ->getParentItem ()) {
359
+ continue ;
360
+ }
361
+ $ _product = $ item ->getProduct ();
364
362
$ _item = [
365
363
'item_id ' => $ item ->getSku (),
366
364
'item_name ' => $ item ->getName (),
367
365
'quantity ' => (int ) $ item ->getQtyOrdered (),
368
- 'price ' => number_format ($ item ->getBasePrice (), 2 , ' . ' , '' ),
369
- 'discount ' => number_format ($ item ->getBaseDiscountAmount (), 2 , ' . ' , '' )
366
+ 'price ' => $ helper -> formatPrice ($ item ->getBasePrice ()),
367
+ 'discount ' => $ helper -> formatPrice ($ item ->getBaseDiscountAmount ())
370
368
];
371
- $ _product = Mage::getModel ('catalog/product ' )->load ($ item ->getProductId ());
372
369
if ($ _product ->getAttributeText ('manufacturer ' )) {
373
370
$ _item ['item_brand ' ] = $ _product ->getAttributeText ('manufacturer ' );
374
371
}
375
-
376
- $ itemCategory = Mage::helper ('googleanalytics ' )->getLastCategoryName ($ _product );
372
+ $ itemCategory = $ helper ->getLastCategoryName ($ _product );
377
373
if ($ itemCategory ) {
378
374
$ _item ['item_category ' ] = $ itemCategory ;
379
375
}
0 commit comments