Skip to content

Commit 110293e

Browse files
committed
Apply feedback, simplify
1 parent 5ad5ab9 commit 110293e

4 files changed

Lines changed: 28 additions & 27 deletions

File tree

classes/Hook/HookActionValidateOrder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ public function run()
6060

6161
// Mark this ID to immediately display it on next page load
6262
$order = $this->params['order'];
63+
64+
// We are checking this, because in case of multishipping, there could be multiple orders
6365
if (!empty($this->context->cookie->__get('ga_admin_order'))) {
64-
$ga_admin_order = $this->context->cookie->__get('ga_admin_order') . ',' . $order->id;
66+
$ga_admin_order = sprintf(
67+
'%1$s,%2$s',
68+
$this->context->cookie->__get('ga_admin_order'),
69+
$order->id
70+
);
6571
} else {
6672
$ga_admin_order = $order->id;
6773
}

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class HookDisplayBackOfficeHeader implements HookInterface
3737
{
3838
private $module;
3939
private $context;
40+
private $gaScripts = '';
4041

4142
public function __construct(Ps_Googleanalytics $module, Context $context)
4243
{
@@ -56,34 +57,31 @@ public function run()
5657
$this->context->controller->addCSS($this->module->getPathUri() . 'views/css/ganalytics.css');
5758
}
5859

59-
$gaScripts = '';
60-
6160
// Render base tag using displayHeader hook with backoffice parameter
62-
$gaScripts .= $this->module->hookDisplayHeader(null, true);
61+
$this->gaScripts .= $this->module->hookDisplayHeader(null, true);
6362

6463
// Process manual orders instantly, we have their IDs in cookie
65-
$gaScripts .= $this->processManualOrders();
64+
$this->processManualOrders();
6665

6766
// Backload old orders that failed to load normally
68-
$gaScripts .= $this->backloadFailedOrders();
67+
$this->processFailedOrders();
6968

70-
return $gaScripts;
69+
return $this->gaScripts;
7170
}
7271

7372
/**
7473
* Checks if there are any orders that failed to be sent normally through front office and processes them
7574
*/
76-
protected function backloadFailedOrders()
75+
protected function processFailedOrders()
7776
{
78-
$gaScripts = '';
7977
if (empty(Configuration::get('GA_BACKLOAD_ENABLED'))) {
80-
return $gaScripts;
78+
return;
8179
}
8280

8381
// Check for value on how long back we will get them
8482
$backloadDays = (int) Configuration::get('GA_BACKLOAD_DAYS');
8583
if ($backloadDays < 1) {
86-
return $gaScripts;
84+
return;
8785
}
8886

8987
// Get all failed orders (either not present in our table or not sent)
@@ -96,34 +94,29 @@ protected function backloadFailedOrders()
9694

9795
// Process each failed order
9896
foreach ($failedOrders as $row) {
99-
$gaScripts .= $this->processOrder((int) $row['id_order']);
97+
$this->processOrder((int) $row['id_order']);
10098
}
101-
102-
return $gaScripts;
10399
}
104100

105101
/**
106102
* Checks if there are any manual orders in cookie and processes them
107103
*/
108104
protected function processManualOrders()
109105
{
110-
$gaScripts = '';
111106
$adminOrders = $this->context->cookie->__get('ga_admin_order');
112107
if (empty($adminOrders)) {
113-
return $gaScripts;
108+
return;
114109
}
115110

116111
// Separate them by IDs and process one by one
117112
$adminOrders = explode(',', $adminOrders);
118113
foreach ($adminOrders as $idOrder) {
119-
$gaScripts .= $this->processOrder((int) $idOrder);
114+
$this->processOrder((int) $idOrder);
120115
}
121116

122117
// Clean up the cookie
123118
$this->context->cookie->__unset('ga_admin_order');
124-
$this->context->cookie->write();
125-
126-
return $gaScripts;
119+
$this->context->cookie->write();;
127120
}
128121

129122
/**
@@ -133,11 +126,10 @@ protected function processManualOrders()
133126
*/
134127
public function processOrder($idOrder)
135128
{
136-
$gaScripts = '';
137129
$order = new Order((int) $idOrder);
138130

139131
if (!Validate::isLoadedObject($order) || $order->getCurrentState() == (int) Configuration::get('PS_OS_ERROR')) {
140-
return $gaScripts;
132+
return;
141133
}
142134

143135
// Load up our handlers and repositories
@@ -152,13 +144,16 @@ public function processOrder($idOrder)
152144
}
153145

154146
// If the order was already sent for some reason, don't do anything
155-
if ($ganalyticsRepository->orderAlreadySent((int) $order->id)) {
156-
return $gaScripts;
147+
if ($ganalyticsRepository->hasOrderBeenAlreadySent((int) $order->id)) {
148+
return;
157149
}
158150

159151
// Prepare transaction data
160152
$orderData = $orderWrapper->wrapOrder($order);
161153

154+
// Empty script for the current order
155+
$gaScripts = '';
156+
162157
// Add payment event
163158
$gaScripts .= $this->module->getTools()->renderEvent(
164159
'add_payment_info',
@@ -184,6 +179,6 @@ public function processOrder($idOrder)
184179
$this->context->link->getAdminLink('AdminGanalyticsAjax')
185180
);
186181

187-
return $gaTagHandler->generate($gaScripts);
182+
$this->gaScripts .= $gaTagHandler->generate($gaScripts);
188183
}
189184
}

classes/Hook/HookDisplayOrderConfirmation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function run()
6868
}
6969

7070
// If the customer is revisiting confirmation screen and the order was already sent, we don't do anything
71-
if ($ganalyticsRepository->orderAlreadySent((int) $order->id)) {
71+
if ($ganalyticsRepository->hasOrderBeenAlreadySent((int) $order->id)) {
7272
return $gaScripts;
7373
}
7474

classes/Repository/GanalyticsRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function findGaOrderByOrderId($orderId)
4848
*
4949
* @return bool
5050
*/
51-
public function orderAlreadySent($idOrder)
51+
public function hasOrderBeenAlreadySent($idOrder)
5252
{
5353
return (bool) Db::getInstance()->getValue(
5454
'SELECT `sent`

0 commit comments

Comments
 (0)