7
7
use Thelia \Model \Base \CurrencyQuery ;
8
8
use Thelia \Model \BrandQuery ;
9
9
use Thelia \Model \CartItem ;
10
+ use Thelia \Model \CartQuery ;
10
11
use Thelia \Model \Category ;
11
12
use Thelia \Model \CategoryQuery ;
12
13
use Thelia \Model \ConfigQuery ;
22
23
use Thelia \Model \ProductSaleElements ;
23
24
use Thelia \Model \ProductSaleElementsQuery ;
24
25
use Thelia \TaxEngine \Calculator ;
26
+ use Thelia \TaxEngine \TaxEngine ;
25
27
26
28
class GoogleTagService
27
29
{
28
30
/**
29
31
* @var RequestStack
30
32
*/
31
33
private $ requestStack ;
34
+ /**
35
+ * @var TaxEngine
36
+ */
37
+ private $ taxEngine ;
32
38
33
- public function __construct (RequestStack $ requestStack )
39
+ public function __construct (RequestStack $ requestStack, TaxEngine $ taxEngine )
34
40
{
35
41
$ this ->requestStack = $ requestStack ;
42
+ $ this ->taxEngine = $ taxEngine ;
36
43
}
37
44
38
45
public function getTheliaPageViewParameters ()
@@ -64,15 +71,15 @@ public function getTheliaPageViewParameters()
64
71
$ result ['google_tag_params ' ]['ecomm_category ' ] = $ this ->getPageName ($ view );
65
72
}
66
73
67
- if (in_array ($ pageType , ['product ' , ' cart ' , ' purchase ' ])) {
74
+ if (in_array ($ pageType , ['product ' ])) {
68
75
$ result ['google_tag_params ' ]['ecomm_prodid ' ] = $ this ->getPageProductRef ($ view );
69
76
}
70
77
71
78
if (in_array ($ pageType , ['cart ' , 'purchase ' ])) {
72
79
$ result ['google_tag_params ' ]['ecomm_totalvalue ' ] = $ this ->getOrderTotalAmount ($ view );
73
80
}
74
81
75
- return json_encode ($ result );
82
+ return json_encode ($ result, JSON_HEX_APOS );
76
83
}
77
84
78
85
public function getProductItem (
@@ -156,7 +163,7 @@ public function getProductItem(
156
163
return $ item ;
157
164
}
158
165
159
- public function getProductItems (array $ productIds , $ itemList = false )
166
+ public function getProductItems (array $ productIds = null , $ itemList = false )
160
167
{
161
168
$ session = $ this ->requestStack ->getCurrentRequest ()->getSession ();
162
169
$ products = ProductQuery::create ()->filterById ($ productIds )->find ();
@@ -196,7 +203,53 @@ public function getLogInData($authAction)
196
203
return json_encode ($ result );
197
204
}
198
205
199
- public function getPurchaseData ($ orderId )
206
+ public function getCartData (int $ cartId , $ addressCountry ): string
207
+ {
208
+ $ cart = CartQuery::create ()->findPk ($ cartId );
209
+
210
+ if (!$ cart ) {
211
+ return json_encode ([]);
212
+ }
213
+
214
+ $ items = array_map (function (CartItem $ cartItem ) use ($ addressCountry ) {
215
+ return $ this ->getProductCartItems ($ cartItem , $ addressCountry );
216
+ }, iterator_to_array ($ cart ->getCartItems ()));
217
+
218
+ return json_encode ([
219
+ 'event ' => 'view_cart ' ,
220
+ 'currency ' => $ cart ->getCurrency ()->getCode (),
221
+ 'value ' => $ cart ->getTaxedAmount ($ addressCountry ),
222
+ 'items ' => $ items
223
+ ], JSON_HEX_APOS );
224
+ }
225
+
226
+ public function getCheckOutData (int $ cartId , $ addressCountry ): string
227
+ {
228
+ $ cart = CartQuery::create ()->findPk ($ cartId );
229
+
230
+ if (!$ cart ) {
231
+ return json_encode ([]);
232
+ }
233
+
234
+ /** @var Session $session */
235
+ $ session = $ this ->requestStack ->getCurrentRequest ()->getSession ();
236
+
237
+ $ coupons = implode (', ' ,$ session ->getConsumedCoupons ());
238
+
239
+ $ items = array_map (function (CartItem $ cartItem ) use ($ addressCountry ) {
240
+ return $ this ->getProductCartItems ($ cartItem , $ addressCountry );
241
+ }, iterator_to_array ($ cart ->getCartItems ()));
242
+
243
+ return json_encode ([
244
+ 'event ' => 'begin_checkout ' ,
245
+ 'currency ' => $ cart ->getCurrency ()->getCode (),
246
+ 'value ' => $ cart ->getTaxedAmount ($ addressCountry ),
247
+ 'coupon ' => $ coupons ,
248
+ 'items ' => $ items
249
+ ], JSON_HEX_APOS );
250
+ }
251
+
252
+ public function getPurchaseData (int $ orderId )
200
253
{
201
254
$ order = OrderQuery::create ()->findPk ($ orderId );
202
255
@@ -235,7 +288,7 @@ public function getPurchaseData($orderId)
235
288
'country ' => $ invoiceAddress ->getCountry ()->getIsoalpha2 ()
236
289
]
237
290
]
238
- ]);
291
+ ], JSON_HEX_APOS );
239
292
}
240
293
241
294
public function getOrderProductItems (Order $ order , Country $ country )
@@ -259,6 +312,20 @@ public function getOrderProductItems(Order $order, Country $country)
259
312
return $ items ;
260
313
}
261
314
315
+ public function getProductCartItems (CartItem $ cartItem , Country $ country )
316
+ {
317
+ $ session = $ this ->requestStack ->getCurrentRequest ()->getSession ();
318
+
319
+ /** @var Lang $lang */
320
+ $ lang = $ session ->get ('thelia.current.lang ' );
321
+
322
+ $ currency = $ session ->getCurrency () ?: CurrencyQuery::create ()->findOneByByDefault (1 );
323
+
324
+ $ product = $ cartItem ->getProductSaleElements ()->getProduct ();
325
+
326
+ return $ this ->getProductItem ($ product , $ lang , $ currency , $ cartItem ->getProductSaleElements (), $ cartItem ->getQuantity (), false , true , $ country );
327
+ }
328
+
262
329
protected function getCategories (Category $ category , $ locale , $ categories )
263
330
{
264
331
if ($ category ->getParent () !== 0 ) {
0 commit comments