Skip to content

Commit 4ce4bf6

Browse files
committed
Finalize cart actions
1 parent 5e03dea commit 4ce4bf6

10 files changed

Lines changed: 65 additions & 58 deletions

classes/Database/Install.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function registerHooks()
110110
$this->module->registerHook('actionProductCancel') &&
111111
$this->module->registerHook('actionValidateOrder') &&
112112
$this->module->registerHook('actionOrderStatusPostUpdate') &&
113-
$this->module->registerHook('actionCartSave') &&
113+
$this->module->registerHook('actionCartUpdateQuantityBefore') &&
114+
$this->module->registerHook('actionObjectProductInCartDeleteBefore') &&
114115
$this->module->registerHook('displayBackOfficeHeader') &&
115116
$this->module->registerHook('actionCarrierProcess');
116117
}

classes/Hook/HookActionCarrierProcess.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

2323
use Context;
24-
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
2524
use PrestaShop\Module\Ps_Googleanalytics\Repository\CarrierRepository;
2625
use Ps_Googleanalytics;
2726

@@ -46,10 +45,6 @@ public function run()
4645
{
4746
if (isset($this->params['cart']->id_carrier)) {
4847
$carrierRepository = new CarrierRepository();
49-
$ganalyticsDataHandler = new GanalyticsDataHandler(
50-
$this->context->cart->id,
51-
$this->context->shop->id
52-
);
5348

5449
// Load carrier name
5550
$carrierName = (string) $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
@@ -65,13 +60,13 @@ public function run()
6560
'value' => (float) $this->context->cart->getSummaryDetails()['total_price'],
6661
'shipping_tier' => $carrierName,
6762
];
68-
$js = $this->module->getTools()->renderEvent(
63+
$jsCode = $this->module->getTools()->renderEvent(
6964
'add_shipping_info',
7065
$eventData
7166
);
7267

7368
// Store it into our repository so we can flush it on next page load
74-
$ganalyticsDataHandler->persistData($js);
69+
$this->module->getDataHandler()->persistData($jsCode);
7570
}
7671
}
7772

classes/Hook/HookActionCartUpdateQuantityBefore.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

2323
use Context;
24-
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
2524
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
2625
use Product;
2726
use Ps_Googleanalytics;
@@ -49,12 +48,6 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4948
*/
5049
public function run()
5150
{
52-
// Get our tag handler
53-
$ganalyticsDataHandler = new GanalyticsDataHandler(
54-
$this->context->cart->id,
55-
$this->context->shop->id
56-
);
57-
5851
/*
5952
* The hook passes a legacy Product object to add, but no attribute information.
6053
* But thankfully, we can use id_product_attribute for this.
@@ -81,9 +74,8 @@ public function run()
8174
if (!empty($this->params['id_product_attribute'])) {
8275
$product['id_product_attribute'] = (int) $this->params['id_product_attribute'];
8376
}
84-
$product['price_amount'] = $product['price'];
8577

86-
// Add informationa about quantity difference
78+
// Add information about quantity difference
8779
$product['quantity'] = (int) $this->params['quantity'];
8880

8981
// Prepare it and format it for our purpose
@@ -93,7 +85,7 @@ public function run()
9385
// Prepare and render event
9486
$eventData = [
9587
'currency' => $this->context->currency->iso_code,
96-
'value' => $product['price_amount'] * $product['quantity'],
88+
'value' => $item['price'] * $item['quantity'],
9789
'items' => [$item],
9890
];
9991
$jsCode = $this->module->getTools()->renderEvent(
@@ -102,7 +94,7 @@ public function run()
10294
);
10395

10496
// Store this event
105-
$ganalyticsDataHandler->persistData($jsCode);
97+
$this->module->getDataHandler()->persistData($jsCode);
10698
}
10799

108100
/**

classes/Hook/HookActionOrderStatusPostUpdate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function run()
7474
// If it was not already refunded
7575
if ($gaRefundSent === false) {
7676
// We refund it and set the "sent" flag to true
77-
$js = $this->getGoogleAnalytics4($this->params['id_order']);
78-
$this->context->cookie->ga_admin_refund = $js;
77+
$jsCode = $this->getGoogleAnalytics4($this->params['id_order']);
78+
$this->context->cookie->ga_admin_refund = $jsCode;
7979
$this->context->cookie->write();
8080

8181
// We save this information to database

classes/Hook/HookActionProductCancel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public function run()
6464
}
6565

6666
$idProduct = empty($orderDetail->product_attribute_id) ? $orderDetail->product_id : $orderDetail->product_id . '-' . $orderDetail->product_attribute_id;
67-
$js = $this->getGoogleAnalytics4(
67+
$jsCode = $this->getGoogleAnalytics4(
6868
(int) $this->params['order']->id,
6969
$idProduct,
7070
(float) $this->params['cancel_quantity'],
7171
$orderDetail->product_name
7272
);
7373

74-
$this->context->cookie->ga_admin_refund = $js;
74+
$this->context->cookie->ga_admin_refund = $jsCode;
7575
$this->context->cookie->write();
7676
}
7777

classes/Hook/HookDisplayBeforeBodyClosingTag.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@
2020

2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

23-
use Configuration;
2423
use Context;
25-
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
2624
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
2725
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
2826
use Ps_Googleanalytics;
29-
use RecursiveArrayIterator;
30-
use RecursiveIteratorIterator;
31-
use Shop;
32-
use Tools;
3327

3428
class HookDisplayBeforeBodyClosingTag implements HookInterface
3529
{
@@ -248,14 +242,8 @@ private function saveInformationAboutListing()
248242
*/
249243
private function outputStoredEvents()
250244
{
251-
// Prepare handler responsible for storing our data
252-
$ganalyticsDataHandler = new GanalyticsDataHandler(
253-
$this->context->cart->id,
254-
$this->context->shop->id
255-
);
256-
257245
// Get all stored events
258-
$storedEvents = $ganalyticsDataHandler->readData();
246+
$storedEvents = $this->module->getDataHandler()->readData();
259247
if (empty($storedEvents)) {
260248
return;
261249
}
@@ -267,6 +255,6 @@ private function outputStoredEvents()
267255
}
268256

269257
// Delete the repository because everything has been flushed
270-
$ganalyticsDataHandler->deleteData();
258+
$this->module->getDataHandler()->deleteData();
271259
}
272260
}

classes/Hook/HookDisplayFooterProduct.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public function run()
6161
$productWrapper = new ProductWrapper($this->context);
6262
$item = $productWrapper->prepareItemFromProduct($product);
6363

64-
$js = '';
64+
$jsCode = '';
6565

6666
// Prepare and render event
6767
$eventData = [
6868
'currency' => $this->context->currency->iso_code,
69-
'value' => $product['price_amount'],
69+
'value' => $item['price'],
7070
'items' => [$item],
7171
];
72-
$js .= $this->module->getTools()->renderEvent(
72+
$jsCode .= $this->module->getTools()->renderEvent(
7373
'view_item',
7474
$eventData
7575
);
@@ -90,13 +90,13 @@ public function run()
9090
}
9191

9292
// Render the event
93-
$js .= $this->module->getTools()->renderEvent(
93+
$jsCode .= $this->module->getTools()->renderEvent(
9494
'select_item',
9595
$eventData
9696
);
9797
}
9898

99-
return $gaTagHandler->generate($js);
99+
return $gaTagHandler->generate($jsCode);
100100
}
101101

102102
/**

classes/Wrapper/ProductWrapper.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public function __construct(Context $context)
4141
* Takes provided list of product (lazy) arrays and converts it to a format that GA4 requires.
4242
*
4343
* @param array $productList
44+
* @param bool $useProvidedQuantity Should provided quantity be used, usually for cart related events
4445
*
4546
* @return array Item data standardized for GA
4647
*/
47-
public function prepareItemListFromProductList($productList, $isCartItem = false)
48+
public function prepareItemListFromProductList($productList, $useProvidedQuantity = false)
4849
{
4950
$items = [];
5051

@@ -56,7 +57,7 @@ public function prepareItemListFromProductList($productList, $isCartItem = false
5657
// Prepare each item and override the counter
5758
$counter = 0;
5859
foreach ($productList as $product) {
59-
$product = $this->prepareItemFromProduct($product, $isCartItem);
60+
$product = $this->prepareItemFromProduct($product, $useProvidedQuantity);
6061
$product['index'] = $counter;
6162
$items[] = $product;
6263
++$counter;
@@ -71,12 +72,14 @@ public function prepareItemListFromProductList($productList, $isCartItem = false
7172
* - ProductListingLazyArray from presented listings
7273
* - ProductListingLazyArray from presented cart
7374
* - Raw $cart->getProducts()
75+
* - Legacy product object converted to an array enriched with Product::getProductProperties
7476
*
7577
* @param ProductLazyArray|ProductListingLazyArray|array $product
78+
* @param bool $useProvidedQuantity Should provided quantity be used, usually for cart related events
7679
*
7780
* @return array Item data standardized for GA
7881
*/
79-
public function prepareItemFromProduct($product, $isCartItem = false)
82+
public function prepareItemFromProduct($product, $useProvidedQuantity = false)
8083
{
8184
// Standardize product ID
8285
$product_id = 0;
@@ -103,23 +106,23 @@ public function prepareItemFromProduct($product, $isCartItem = false)
103106
// Add manufacturer info if we have it
104107
if (!empty($product['manufacturer_name'])) {
105108
$item['item_brand'] = $product['manufacturer_name'];
109+
// TODO - missing in some events?
106110
}
107111

108-
if ($isCartItem === true) {
109-
if (!empty($product['id_product_attribute'])) {
110-
$item['item_id'] .= '-' . $product['id_product_attribute'];
111-
}
112+
// We will specify variant ID if we have it
113+
if (!empty($product['id_product_attribute'])) {
114+
$item['item_id'] .= '-' . $product['id_product_attribute'];
115+
}
112116

113-
// Info about quantity in cart, if we have it
114-
if (isset($product['cart_quantity'])) {
115-
$item['quantity'] = $product['quantity'];
116-
}
117+
// Information about a chosen variant, if we have it
118+
if (!empty($product['attributes_small'])) {
119+
$item['item_variant'] = $product['attributes_small'];
120+
// TODO - get manually if missing and we have id_product_attribute
121+
}
117122

118-
// In case of products from a cart, we will add more information
119-
// Information about a chosen variant, if we have it
120-
if (!empty($product['attributes_small'])) {
121-
$item['item_variant'] = $product['attributes_small'];
122-
}
123+
if ($useProvidedQuantity === true) {
124+
// Info about quantity in cart, if we have it
125+
$item['quantity'] = $product['quantity'];
123126
}
124127

125128
// Prepare category information, put default category as the main one

ps_googleanalytics.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Ps_Googleanalytics extends Module
4141
public $products = [];
4242
public $_debug = 0;
4343
private $tools = null;
44+
private $dataHandler = null;
4445

4546
public function __construct()
4647
{
@@ -187,6 +188,17 @@ public function hookActionCartUpdateQuantityBefore($params)
187188
$hook->run();
188189
}
189190

191+
/**
192+
* Hook to process remove items from cart events
193+
* This function is run to implement 'remove from cart' functionalities
194+
*/
195+
public function hookActionObjectProductInCartDeleteBefore($params)
196+
{
197+
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionObjectProductInCartDeleteBefore($this, $this->context);
198+
$hook->setParams($params);
199+
$hook->run();
200+
}
201+
190202
public function hookActionCarrierProcess($params)
191203
{
192204
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionCarrierProcess($this, $this->context);
@@ -251,4 +263,19 @@ public function getTools()
251263

252264
return $this->tools;
253265
}
266+
267+
/**
268+
* Returns instance of GanalyticsDataHandler
269+
*/
270+
public function getDataHandler()
271+
{
272+
if ($this->dataHandler === null) {
273+
$this->dataHandler = new PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler(
274+
$this->context->cart->id,
275+
$this->context->shop->id
276+
);
277+
}
278+
279+
return $this->dataHandler;
280+
}
254281
}

upgrade/upgrade-5.0.0.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function upgrade_module_5_0_0($object)
3333
$object->registerHook('actionValidateOrder') &&
3434
$object->unregisterHook('actionCartSave') &&
3535
$object->registerHook('actionCartUpdateQuantityBefore') &&
36+
$object->registerHook('actionObjectProductInCartDeleteBefore') &&
3637
$database->installTab() &&
3738
Configuration::updateValue('GA_BACKLOAD_ENABLED', false) &&
3839
Configuration::updateValue('GA_BACKLOAD_DAYS', 30);

0 commit comments

Comments
 (0)