Skip to content

Commit 03f3dd4

Browse files
committed
Initial commit
Fix Fix Fix forgotten class Fix style Fix version number in upgrade Update code styling Co-authored-by: GoT <PierreRambaud@users.noreply.github.com> Update condition style Co-authored-by: GoT <PierreRambaud@users.noreply.github.com> Add settings to configure cancelled states Code styling fix Cookie must be saved permanently Fix extra = Implementing code suggestions Initial commit
1 parent c6e71f9 commit 03f3dd4

5 files changed

Lines changed: 190 additions & 2 deletions

File tree

classes/Database/Install.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace PrestaShop\Module\Ps_Googleanalytics\Database;
2222

23+
use Configuration;
2324
use Db;
2425
use Ps_Googleanalytics;
2526
use Shop;
@@ -55,6 +56,7 @@ public function installTables()
5556
`id_customer` int(10) NOT NULL,
5657
`id_shop` int(11) NOT NULL,
5758
`sent` tinyint(1) DEFAULT NULL,
59+
`refund_sent` tinyint(1) DEFAULT NULL,
5860
`date_add` datetime DEFAULT NULL,
5961
PRIMARY KEY (`id_google_analytics`),
6062
KEY `id_order` (`id_order`),
@@ -77,6 +79,18 @@ public function installTables()
7779
return true;
7880
}
7981

82+
/**
83+
* Insert default data to database
84+
*
85+
* @return bool
86+
*/
87+
public function setDefaultConfiguration()
88+
{
89+
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
90+
91+
return true;
92+
}
93+
8094
/**
8195
* Register Module hooks
8296
*
@@ -91,6 +105,7 @@ public function registerHooks()
91105
$this->module->registerHook('displayFooterProduct') &&
92106
$this->module->registerHook('displayOrderConfirmation') &&
93107
$this->module->registerHook('actionProductCancel') &&
108+
$this->module->registerHook('actionOrderStatusPostUpdate') &&
94109
$this->module->registerHook('actionCartSave') &&
95110
$this->module->registerHook('displayBackOfficeHeader') &&
96111
$this->module->registerHook('actionCarrierProcess');

classes/Form/ConfigurationForm.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
use AdminController;
2424
use Configuration;
25+
use Context;
2526
use HelperForm;
27+
use OrderState;
2628
use Ps_Googleanalytics;
2729
use Shop;
2830
use Tools;
@@ -145,6 +147,19 @@ public function generate()
145147
],
146148
],
147149
],
150+
[
151+
'type' => 'select',
152+
'label' => $this->module->l('Cancelled order states'),
153+
'name' => 'GA_CANCELLED_STATES',
154+
'desc' => $this->module->l('Choose order states, in which you consider the given order cancelled. This will be usually only the default "Cancelled" state, but some shops may have extra states like "Returned" etc.'),
155+
'class' => 'chosen',
156+
'multiple' => true,
157+
'options' => [
158+
'query' => OrderState::getOrderStates((int) Context::getContext()->language->id),
159+
'id' => 'id_order_state',
160+
'name' => 'name',
161+
],
162+
],
148163
],
149164
'submit' => [
150165
'title' => $this->module->l('Save'),
@@ -177,6 +192,7 @@ public function generate()
177192
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
178193
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
179194
$helper->fields_value['GA_TRACK_BACKOFFICE_DISABLED'] = Configuration::get('GA_TRACK_BACKOFFICE_DISABLED');
195+
$helper->fields_value['GA_CANCELLED_STATES[]'] = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
180196

181197
return $helper->generateForm($fields_form);
182198
}
@@ -194,6 +210,7 @@ public function treat()
194210
$gaCrossdomainEnabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
195211
$gaAnonymizeEnabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
196212
$gaTrackBackOffice = Tools::getValue('GA_TRACK_BACKOFFICE_DISABLED');
213+
$gaCancelledStates = Tools::getValue('GA_CANCELLED_STATES');
197214

198215
if (!empty($gaAccountId)) {
199216
Configuration::updateValue('GA_ACCOUNT_ID', $gaAccountId);
@@ -221,6 +238,13 @@ public function treat()
221238
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Disable Back Office tracking updated successfully'));
222239
}
223240

241+
if ($gaCancelledStates === false) {
242+
Configuration::updateValue('GA_CANCELLED_STATES', '');
243+
} else {
244+
Configuration::updateValue('GA_CANCELLED_STATES', json_encode($gaCancelledStates));
245+
}
246+
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for cancelled order states updated successfully'));
247+
224248
return $treatmentResult;
225249
}
226250
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* 2007-2020 PrestaShop and Contributors
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/AFL-3.0
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@prestashop.com so we can send you a copy immediately.
14+
*
15+
* @author PrestaShop SA <contact@prestashop.com>
16+
* @copyright 2007-2020 PrestaShop SA and Contributors
17+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
18+
* International Registered Trademark & Property of PrestaShop SA
19+
*/
20+
21+
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
22+
23+
use Configuration;
24+
use Context;
25+
use Db;
26+
use Ps_Googleanalytics;
27+
28+
class HookActionOrderStatusPostUpdate implements HookInterface
29+
{
30+
/**
31+
* @var Ps_Googleanalytics
32+
*/
33+
private $module;
34+
35+
/**
36+
* @var Context
37+
*/
38+
private $context;
39+
40+
/**
41+
* @var array
42+
*/
43+
private $params;
44+
45+
public function __construct(Ps_Googleanalytics $module, Context $context)
46+
{
47+
$this->module = $module;
48+
$this->context = $context;
49+
}
50+
51+
/**
52+
* run
53+
*
54+
* @return void
55+
*/
56+
public function run()
57+
{
58+
// If we do not have an order or a new order status, we return
59+
if (empty($this->params['id_order']) || empty($this->params['newOrderStatus']->id)) {
60+
return;
61+
}
62+
63+
// We get all states in which the merchant want to have refund sent and check if the new state being set belongs there
64+
$gaCancelledStates = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
65+
if (empty($gaCancelledStates) || !in_array($this->params['newOrderStatus']->id, $gaCancelledStates)) {
66+
return;
67+
}
68+
69+
// We check if the refund was already sent to Google Analytics
70+
$gaRefundSent = Db::getInstance()->getValue(
71+
'SELECT id_order FROM `' . _DB_PREFIX_ . 'ganalytics` WHERE id_order = ' . (int) $this->params['id_order'] . ' AND refund_sent = 1'
72+
);
73+
74+
// If it was not already refunded
75+
if ($gaRefundSent === false) {
76+
// We refund it and set the "sent" flag to true
77+
$this->context->cookie->__set('ga_admin_refund', 'MBG.refundByOrderId(' . json_encode(['id' => $this->params['id_order']]) . ');');
78+
$this->context->cookie->write();
79+
80+
// We save this information to database
81+
Db::getInstance()->execute(
82+
'UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET refund_sent = 1 WHERE id_order = ' . (int) $this->params['id_order']
83+
);
84+
}
85+
}
86+
87+
/**
88+
* setParams
89+
*
90+
* @param array $params
91+
*/
92+
public function setParams($params)
93+
{
94+
$this->params = $params;
95+
}
96+
}

ps_googleanalytics.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct()
5151
{
5252
$this->name = 'ps_googleanalytics';
5353
$this->tab = 'analytics_stats';
54-
$this->version = '4.0.0';
54+
$this->version = '4.1.0';
5555
$this->ps_versions_compliancy = ['min' => '1.6', 'max' => _PS_VERSION_];
5656
$this->author = 'PrestaShop';
5757
$this->module_key = 'fd2aaefea84ac1bb512e6f1878d990b8';
@@ -139,11 +139,14 @@ public function hookDisplayAdminOrder()
139139
{
140140
$gaTagHandler = new PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler($this, $this->context);
141141

142-
echo $gaTagHandler->generate(
142+
$output = $gaTagHandler->generate(
143143
$this->context->cookie->__get('ga_admin_refund'),
144144
true
145145
);
146146
$this->context->cookie->__unset('ga_admin_refund');
147+
$this->context->cookie->write();
148+
149+
return $output;
147150
}
148151

149152
/**
@@ -166,6 +169,16 @@ public function hookActionProductCancel($params)
166169
$hook->run();
167170
}
168171

172+
/**
173+
* Hook called after order status change, used to "refund" order after cancelling it
174+
*/
175+
public function hookActionOrderStatusPostUpdate($params)
176+
{
177+
$hook = new PrestaShop\Module\Ps_Googleanalytics\Hooks\HookActionOrderStatusPostUpdate($this, $this->context);
178+
$hook->setParams($params);
179+
$hook->run();
180+
}
181+
169182
/**
170183
* hook save cart event to implement addtocart and remove from cart functionality
171184
*/
@@ -211,6 +224,7 @@ public function install()
211224

212225
return parent::install() &&
213226
$database->registerHooks() &&
227+
$database->setDefaultConfiguration() &&
214228
$database->installTables();
215229
}
216230

upgrade/upgrade-4.1.0.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* 2007-2020 PrestaShop.
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/AFL-3.0
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@prestashop.com so we can send you a copy immediately.
14+
*
15+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18+
* versions in the future. If you wish to customize PrestaShop for your
19+
* needs please refer to http://www.prestashop.com for more information.
20+
*
21+
* @author PrestaShop SA <contact@prestashop.com>
22+
* @copyright 2007-2020 PrestaShop SA
23+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
24+
* International Registered Trademark & Property of PrestaShop SA
25+
*/
26+
if (!defined('_PS_VERSION_')) {
27+
exit;
28+
}
29+
30+
function upgrade_module_4_1_0($object)
31+
{
32+
if (!$object->registerHook('actionOrderStatusPostUpdate')) {
33+
return false;
34+
}
35+
36+
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
37+
38+
return Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . 'ganalytics` ADD `refund_sent` tinyint(1) NULL AFTER `sent`;');
39+
}

0 commit comments

Comments
 (0)