/
ps_shoppingcart.php
295 lines (266 loc) · 9.67 KB
/
ps_shoppingcart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\PrestaShop\Adapter\Presenter\Cart\CartPresenter;
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class Ps_Shoppingcart extends Module implements WidgetInterface
{
/**
* @var string Name of the module running on PS 1.6.x. Used for data migration.
*/
const PS_16_EQUIVALENT_MODULE = 'blockcart';
public function __construct()
{
$this->name = 'ps_shoppingcart';
$this->tab = 'front_office_features';
$this->version = '3.0.0';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Shopping cart', [], 'Modules.Shoppingcart.Admin');
$this->description = $this->trans('Display a shopping cart icon on your pages and the number of items it contains.', [], 'Modules.Shoppingcart.Admin');
$this->ps_versions_compliancy = ['min' => '1.7.5.0', 'max' => _PS_VERSION_];
$this->controllers = ['ajax'];
}
/**
* @return void
*/
public function hookDisplayHeader()
{
if (Configuration::isCatalogMode()) {
return;
}
if (Configuration::get('PS_BLOCK_CART_AJAX')) {
$this->context->controller->registerJavascript('modules-shoppingcart', 'modules/' . $this->name . '/ps_shoppingcart.js', ['position' => 'bottom', 'priority' => 150]);
}
}
/**
* @return string
*/
private function getCartSummaryURL()
{
return $this->context->link->getPageLink(
'cart',
null,
$this->context->language->id,
[
'action' => 'show',
],
false,
null,
true
);
}
/**
* @param string|null $hookName
* @param array<string,mixed> $params
*
* @return array<string,mixed>
*/
public function getWidgetVariables($hookName, array $params)
{
return [
'cart' => $this->getPresentedCart(),
'refresh_url' => $this->context->link->getModuleLink('ps_shoppingcart', 'ajax', [], null, null, null, true),
'cart_url' => $this->getCartSummaryURL(),
];
}
/**
* Provides an already presented object from the context if set.
* If not, runs the presenter.
*
* @return array presented cart
*/
private function getPresentedCart()
{
/*
* We will use the already presented cart in the first place. It should be already in the template.
* Check FrontController::assignGeneralPurposeVariables for more information.
*
* If it's not, we will present the cart ourselves. There will always be a cart object
* assigned in FrontController::init.
*/
if (!empty($this->context->smarty->getTemplateVars('cart'))) {
return $this->context->smarty->getTemplateVars('cart');
} else {
return (new CartPresenter())->present($this->context->cart);
}
}
/**
* @param string|null $hookName
* @param array<string,mixed> $params
*
* @return string
*/
public function renderWidget($hookName, array $params)
{
if (Configuration::isCatalogMode()) {
return '';
}
$this->smarty->assign($this->getWidgetVariables($hookName, $params));
return $this->fetch('module:ps_shoppingcart/ps_shoppingcart.tpl');
}
/**
* @param int $id_product
* @param int $id_product_attribute
* @param int $id_customization
*
* @return string
*
* @throws Exception
*/
public function renderModal($id_product, $id_product_attribute, $id_customization)
{
$data = $this->getPresentedCart();
$product = null;
foreach ($data['products'] as $p) {
if ((int) $p['id_product'] == $id_product &&
(int) $p['id_product_attribute'] == $id_product_attribute &&
(int) $p['id_customization'] == $id_customization) {
$product = $p;
break;
}
}
$this->smarty->assign([
'product' => $product,
'cart' => $data,
'cart_url' => $this->getCartSummaryURL(),
]);
return $this->fetch('module:ps_shoppingcart/modal.tpl');
}
/**
* @return string
*/
public function getContent()
{
$output = '';
if (Tools::isSubmit('submitBlockCart')) {
$ajax = Tools::getValue('PS_BLOCK_CART_AJAX');
if ($ajax != 0 && $ajax != 1) {
$output .= $this->displayError($this->trans('Ajax: Invalid choice.', [], 'Modules.Shoppingcart.Admin'));
} else {
Configuration::updateValue('PS_BLOCK_CART_AJAX', (int) ($ajax));
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->name . '&conf=6');
}
}
return $output . $this->renderForm();
}
/**
* @return bool
*/
public function install()
{
$this->uninstallPrestaShop16Module();
return
parent::install()
&& $this->registerHook('displayHeader')
&& $this->registerHook('displayNav2')
&& Configuration::updateValue('PS_BLOCK_CART_AJAX', 1);
}
/**
* Migrate data from 1.6 equivalent module (if applicable), then uninstall
*
* @return bool
*/
public function uninstallPrestaShop16Module()
{
if (!Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
return false;
}
$oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
if ($oldModule) {
// This closure calls the parent class to prevent data to be erased
// It allows the new module to be configured without migration
$parentUninstallClosure = function () {
return parent::uninstall();
};
$parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule));
$parentUninstallClosure();
}
return true;
}
/**
* @return string
*/
public function renderForm()
{
$fields_form = [
'form' => [
'legend' => [
'title' => $this->trans('Settings', [], 'Admin.Global'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'switch',
'label' => $this->trans('Ajax cart', [], 'Modules.Shoppingcart.Admin'),
'name' => 'PS_BLOCK_CART_AJAX',
'is_bool' => true,
'desc' => $this->trans('Activate Ajax mode for the cart (compatible with the default theme).', [], 'Modules.Shoppingcart.Admin'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Yes', [], 'Admin.Global'),
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('No', [], 'Admin.Global'),
],
],
],
],
'submit' => [
'title' => $this->trans('Save', [], 'Admin.Actions'),
],
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitBlockCart';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab
. '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = [
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$fields_form]);
}
/**
* @return bool[]
*/
public function getConfigFieldsValues()
{
return [
'PS_BLOCK_CART_AJAX' => (bool) Tools::getValue('PS_BLOCK_CART_AJAX', Configuration::get('PS_BLOCK_CART_AJAX')),
];
}
}