· 8 years ago · May 20, 2018, 01:32 PM
1<?php
2/**
3 * 2007-2016 PrestaShop
4 * 2017 - thirty bees
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Academic Free License (AFL 3.0)
9 * that is bundled with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://opensource.org/licenses/afl-3.0.php
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to info@thirtybees.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to http://www.prestashop.com for more information.
21 *
22 * @author PrestaShop SA <contact@prestashop.com>
23 * @author thirty bees <info@thirtybees.com>
24 * @copyright 2007-2016 PrestaShop SA
25 * @copyright 2017 - thirty bees
26 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
27 * International Registered Trademark & Property of PrestaShop SA
28 * U.S.A. Trademark of thirty bees
29 */
30
31use MailAlertModule\MailAlert;
32
33if (!defined('_CAN_LOAD_FILES_')) {
34 exit;
35}
36
37require_once __DIR__.'/classes/autoload.php';
38
39/**
40 * Class MailAlerts
41 */
42class MailAlerts extends Module
43{
44 const __MA_MAIL_DELIMITOR__ = "\n";
45 protected $html = '';
46 protected $merchant_mails;
47 protected $merchant_order;
48 protected $merchant_oos;
49 protected $customer_qty;
50 protected $merchant_coverage;
51 protected $product_coverage;
52 protected $order_edited;
53 protected $return_slip;
54
55 /**
56 * MailAlerts constructor.
57 */
58 public function __construct()
59 {
60 $this->name = 'mailalerts';
61 $this->tab = 'administration';
62 $this->version = '4.0.4';
63 $this->author = 'thirty bees';
64 $this->need_instance = 0;
65
66 $this->controllers = ['account'];
67
68 $this->bootstrap = true;
69 parent::__construct();
70
71 if ($this->id) {
72 $this->init();
73 }
74
75 $this->displayName = $this->l('Mail alerts');
76 $this->description = $this->l('Sends e-mail notifications to customers and merchants.');
77 $this->confirmUninstall = $this->l('Are you sure you want to delete all customer notifications?');
78 }
79
80 /**
81 * Initialize
82 */
83 protected function init()
84 {
85 $this->merchant_mails = str_replace(',', self::__MA_MAIL_DELIMITOR__, (string) Configuration::get('MA_MERCHANT_MAILS'));
86 $this->bcc_mails = str_replace(',', self::__MA_MAIL_DELIMITOR__, (string) Configuration::get('MA_BCC'));
87 $this->merchant_order = (int) Configuration::get('MA_MERCHANT_ORDER');
88 $this->merchant_oos = (int) Configuration::get('MA_MERCHANT_OOS');
89 $this->customer_qty = (int) Configuration::get('MA_CUSTOMER_QTY');
90 $this->merchant_coverage = (int) Configuration::getGlobalValue('MA_MERCHANT_COVERAGE');
91 $this->product_coverage = (int) Configuration::getGlobalValue('MA_PRODUCT_COVERAGE');
92 $this->order_edited = (int) Configuration::getGlobalValue('MA_ORDER_EDIT');
93 $this->return_slip = (int) Configuration::getGlobalValue('MA_RETURN_SLIP');
94 }
95
96 /**
97 * @return bool
98 */
99 public function reset()
100 {
101 if (!$this->uninstall(false)) {
102 return false;
103 }
104 if (!$this->install(false)) {
105 return false;
106 }
107
108 return true;
109 }
110
111 /**
112 * Uninstall this module
113 *
114 * @param bool $deleteParams
115 *
116 * @return bool
117 */
118 public function uninstall($deleteParams = true)
119 {
120 if ($deleteParams) {
121 Configuration::deleteByName('MA_MERCHANT_ORDER');
122 Configuration::deleteByName('MA_MERCHANT_OOS');
123 Configuration::deleteByName('MA_CUSTOMER_QTY');
124 Configuration::deleteByName('MA_MERCHANT_MAILS');
125 Configuration::deleteByName('MA_LAST_QTIES');
126 Configuration::deleteByName('MA_MERCHANT_COVERAGE');
127 Configuration::deleteByName('MA_PRODUCT_COVERAGE');
128 Configuration::deleteByName('MA_ORDER_EDIT');
129 Configuration::deleteByName('MA_RETURN_SLIP');
130 Configuration::deleteByName('MA_BCC');
131
132 if (!Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.MailAlert::$definition['table'])) {
133 return false;
134 }
135 }
136
137 return parent::uninstall();
138 }
139
140 /**
141 * @param bool $deleteParams
142 *
143 * @return bool
144 */
145 public function install($deleteParams = true)
146 {
147 if (!parent::install() ||
148 !$this->registerHook('actionValidateOrder') ||
149 !$this->registerHook('actionUpdateQuantity') ||
150 !$this->registerHook('actionProductOutOfStock') ||
151 !$this->registerHook('displayCustomerAccount') ||
152 !$this->registerHook('displayMyAccountBlock') ||
153 !$this->registerHook('actionProductDelete') ||
154 !$this->registerHook('actionProductAttributeDelete') ||
155 !$this->registerHook('actionProductAttributeUpdate') ||
156 !$this->registerHook('actionProductCoverage') ||
157 !$this->registerHook('actionOrderReturn') ||
158 !$this->registerHook('actionOrderEdited') ||
159 !$this->registerHook('displayHeader')
160 ) {
161 return false;
162 }
163
164 if ($deleteParams) {
165 Configuration::updateValue('MA_MERCHANT_ORDER', 1);
166 Configuration::updateValue('MA_MERCHANT_OOS', 1);
167 Configuration::updateValue('MA_CUSTOMER_QTY', 1);
168 Configuration::updateValue('MA_ORDER_EDIT', 1);
169 Configuration::updateValue('MA_RETURN_SLIP', 1);
170 Configuration::updateValue('MA_MERCHANT_MAILS', Configuration::get('PS_SHOP_EMAIL'));
171 Configuration::updateValue('MA_LAST_QTIES', (int) Configuration::get('PS_LAST_QTIES'));
172 Configuration::updateGlobalValue('MA_MERCHANT_COVERAGE', 0);
173 Configuration::updateGlobalValue('MA_PRODUCT_COVERAGE', 0);
174
175 $sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.MailAlert::$definition['table'].'`
176 (
177 `id_customer` int(10) unsigned NOT NULL,
178 `customer_email` varchar(128) NOT NULL,
179 `id_product` int(10) unsigned NOT NULL,
180 `id_product_attribute` int(10) unsigned NOT NULL,
181 `id_shop` int(10) unsigned NOT NULL,
182 `id_lang` int(10) unsigned NOT NULL,
183 PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`)
184 ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
185
186 if (!Db::getInstance()->execute($sql)) {
187 return false;
188 }
189 }
190
191 return true;
192 }
193
194 /**
195 * Module configuration page
196 *
197 * @return string Module HTML
198 */
199 public function getContent()
200 {
201 $this->html = '';
202
203 $this->postProcess();
204
205 $this->html .= $this->renderForm();
206
207 return $this->html;
208 }
209
210 /**
211 * Process module configuration
212 */
213 protected function postProcess()
214 {
215 $errors = [];
216
217 if (Tools::isSubmit('submitMailAlert')) {
218 if (!Configuration::updateValue('MA_CUSTOMER_QTY', (int) Tools::getValue('MA_CUSTOMER_QTY'))) {
219 $errors[] = $this->l('Cannot update settings');
220 }
221 } else {
222 if (Tools::isSubmit('submitMAMerchant')) {
223 $emails = (string) Tools::getValue('MA_MERCHANT_MAILS');
224 $bccs = (string) Tools::getValue('MA_BCC');
225 if (!$emails || empty($emails)) {
226 $errors[] = $this->l('Please type one (or more) e-mail address');
227 } else {
228 $emails = str_replace(',', self::__MA_MAIL_DELIMITOR__, $emails);
229 $emails = explode(self::__MA_MAIL_DELIMITOR__, $emails);
230 foreach ($emails as $k => $email) {
231 $email = trim($email);
232 if (!empty($email) && !Validate::isEmail($email)) {
233 $errors[] = $this->l('Invalid e-mail:').' '.Tools::safeOutput($email);
234 break;
235 } elseif (!empty($email) && count($email) > 0) {
236 $emails[$k] = $email;
237 } else {
238 unset($emails[$k]);
239 }
240 }
241 if ($bccs) {
242 $bccs = str_replace(',', self::__MA_MAIL_DELIMITOR__, $bccs);
243 $bccs = explode(self::__MA_MAIL_DELIMITOR__, $bccs);
244 foreach ($bccs as $k => $bcc) {
245 $bcc = trim($bcc);
246 if (!empty($bcc) && !Validate::isEmail($bcc)) {
247 $errors[] = $this->l('Invalid e-mail:') . ' ' . Tools::safeOutput($email);
248 break;
249 } elseif (!empty($bcc) && count($bcc) > 0) {
250 $bccs[$k] = $bcc;
251 } else {
252 unset($bccs[$k]);
253 }
254 }
255 }
256 $emails = implode(self::__MA_MAIL_DELIMITOR__, $emails);
257
258 if (!Configuration::updateValue('MA_MERCHANT_MAILS', (string) $emails)) {
259 $errors[] = $this->l('Cannot update settings');
260 } elseif (!Configuration::updateValue('MA_MERCHANT_ORDER', (int) Tools::getValue('MA_MERCHANT_ORDER'))) {
261 $errors[] = $this->l('Cannot update settings');
262 } elseif (!Configuration::updateValue('MA_MERCHANT_OOS', (int) Tools::getValue('MA_MERCHANT_OOS'))) {
263 $errors[] = $this->l('Cannot update settings');
264 } elseif (!Configuration::updateValue('MA_LAST_QTIES', (int) Tools::getValue('MA_LAST_QTIES'))) {
265 $errors[] = $this->l('Cannot update settings');
266 } elseif (!Configuration::updateGlobalValue('MA_MERCHANT_COVERAGE', (int) Tools::getValue('MA_MERCHANT_COVERAGE'))) {
267 $errors[] = $this->l('Cannot update settings');
268 } elseif (!Configuration::updateGlobalValue('MA_PRODUCT_COVERAGE', (int) Tools::getValue('MA_PRODUCT_COVERAGE'))) {
269 $errors[] = $this->l('Cannot update settings');
270 } elseif (!Configuration::updateGlobalValue('MA_ORDER_EDIT', (int) Tools::getValue('MA_ORDER_EDIT'))) {
271 $errors[] = $this->l('Cannot update settings');
272 } elseif (!Configuration::updateGlobalValue('MA_RETURN_SLIP', (int) Tools::getValue('MA_RETURN_SLIP'))) {
273 $errors[] = $this->l('Cannot update settings');
274 } elseif (!Configuration::updateGlobalValue('MA_BCC', (string) Tools::getValue('MA_BCC'))) {
275 }
276 }
277 }
278 }
279
280 if (count($errors) > 0) {
281 $this->html .= $this->displayError(implode('<br />', $errors));
282 } else {
283 $this->html .= $this->displayConfirmation($this->l('Settings updated successfully'));
284 }
285
286 $this->init();
287 }
288
289 /**
290 * @return string
291 */
292 public function renderForm()
293 {
294 $fieldsForm1 = [
295 'form' => [
296 'legend' => [
297 'title' => $this->l('Customer notifications'),
298 'icon' => 'icon-cogs',
299 ],
300 'input' => [
301 [
302 'type' => 'switch',
303 'is_bool' => true, //retro compat 1.5
304 'label' => $this->l('Product availability'),
305 'name' => 'MA_CUSTOMER_QTY',
306 'desc' => $this->l('Gives the customer the option of receiving a notification when an out-of-stock product is available again.'),
307 'values' => [
308 [
309 'id' => 'active_on',
310 'value' => 1,
311 'label' => $this->l('Enabled'),
312 ],
313 [
314 'id' => 'active_off',
315 'value' => 0,
316 'label' => $this->l('Disabled'),
317 ],
318 ],
319 ],
320 [
321 'type' => 'switch',
322 'is_bool' => true, //retro compat 1.5
323 'label' => $this->l('Order edit'),
324 'name' => 'MA_ORDER_EDIT',
325 'desc' => $this->l('Send a notification to the customer when an order is edited.'),
326 'values' => [
327 [
328 'id' => 'active_on',
329 'value' => 1,
330 'label' => $this->l('Enabled'),
331 ],
332 [
333 'id' => 'active_off',
334 'value' => 0,
335 'label' => $this->l('Disabled'),
336 ],
337 ],
338 ],
339 ],
340 'submit' => [
341 'title' => $this->l('Save'),
342 'class' => 'btn btn-default pull-right',
343 'name' => 'submitMailAlert',
344 ],
345 ],
346 ];
347
348 $fieldsForm2 = [
349 'form' => [
350 'legend' => [
351 'title' => $this->l('Merchant notifications'),
352 'icon' => 'icon-cogs',
353 ],
354 'input' => [
355 [
356 'type' => 'switch',
357 'is_bool' => true, //retro compat 1.5
358 'label' => $this->l('New order'),
359 'name' => 'MA_MERCHANT_ORDER',
360 'desc' => $this->l('Receive a notification when an order is placed.'),
361 'values' => [
362 [
363 'id' => 'active_on',
364 'value' => 1,
365 'label' => $this->l('Enabled'),
366 ],
367 [
368 'id' => 'active_off',
369 'value' => 0,
370 'label' => $this->l('Disabled'),
371 ],
372 ],
373 ],
374 [
375 'type' => 'switch',
376 'is_bool' => true, //retro compat 1.5
377 'label' => $this->l('Out of stock'),
378 'name' => 'MA_MERCHANT_OOS',
379 'desc' => $this->l('Receive a notification if the available quantity of a product is below the following threshold.'),
380 'values' => [
381 [
382 'id' => 'active_on',
383 'value' => 1,
384 'label' => $this->l('Enabled'),
385 ],
386 [
387 'id' => 'active_off',
388 'value' => 0,
389 'label' => $this->l('Disabled'),
390 ],
391 ],
392 ],
393 [
394 'type' => 'text',
395 'label' => $this->l('Threshold'),
396 'name' => 'MA_LAST_QTIES',
397 'class' => 'fixed-width-xs',
398 'desc' => $this->l('Quantity for which a product is considered out of stock.'),
399 ],
400 [
401 'type' => 'switch',
402 'is_bool' => true, //retro compat 1.5
403 'label' => $this->l('Coverage warning'),
404 'name' => 'MA_MERCHANT_COVERAGE',
405 'desc' => $this->l('Receive a notification when a product has insufficient coverage.'),
406 'values' => [
407 [
408 'id' => 'active_on',
409 'value' => 1,
410 'label' => $this->l('Enabled'),
411 ],
412 [
413 'id' => 'active_off',
414 'value' => 0,
415 'label' => $this->l('Disabled'),
416 ],
417 ],
418 ],
419 [
420 'type' => 'text',
421 'label' => $this->l('Coverage'),
422 'name' => 'MA_PRODUCT_COVERAGE',
423 'class' => 'fixed-width-xs',
424 'desc' => $this->l('Stock coverage, in days. Also, the stock coverage of a given product will be calculated based on this number.'),
425 ],
426 [
427 'type' => 'switch',
428 'is_bool' => true, //retro compat 1.5
429 'label' => $this->l('Returns'),
430 'name' => 'MA_RETURN_SLIP',
431 'desc' => $this->l('Receive a notification when a customer requests a merchandise return.'),
432 'values' => [
433 [
434 'id' => 'active_on',
435 'value' => 1,
436 'label' => $this->l('Enabled'),
437 ],
438 [
439 'id' => 'active_off',
440 'value' => 0,
441 'label' => $this->l('Disabled'),
442 ],
443 ],
444 ],
445 [
446 'type' => 'textarea',
447 'cols' => 36,
448 'rows' => 4,
449 'label' => $this->l('E-mail addresses'),
450 'name' => 'MA_MERCHANT_MAILS',
451 'desc' => $this->l('One e-mail address per line (e.g. bob@example.com).'),
452 ],
453 [
454 'type' => 'textarea',
455 'cols' => 36,
456 'rows' => 4,
457 'label' => $this->l('Order Emails BCC'),
458 'name' => 'MA_BCC',
459 'desc' => $this->l('One e-mail address per line (e.g. bob@example.com).'),
460 ],
461 ],
462 'submit' => [
463 'title' => $this->l('Save'),
464 'class' => 'btn btn-default pull-right',
465 'name' => 'submitMAMerchant',
466 ],
467 ],
468 ];
469
470 $helper = new HelperForm();
471 $helper->show_toolbar = false;
472 $helper->table = $this->table;
473 $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
474 $helper->default_form_language = $lang->id;
475 $helper->module = $this;
476 $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
477 $helper->identifier = $this->identifier;
478 $helper->submit_action = 'submitMailAlertConfiguration';
479 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
480 .'&configure='.$this->name
481 .'&tab_module='.$this->tab
482 .'&module_name='.$this->name;
483 $helper->token = Tools::getAdminTokenLite('AdminModules');
484 $helper->tpl_vars = [
485 'fields_value' => $this->getConfigFieldsValues(),
486 'languages' => $this->context->controller->getLanguages(),
487 'id_language' => $this->context->language->id,
488 ];
489
490 return $helper->generateForm([$fieldsForm1, $fieldsForm2]);
491 }
492
493 /**
494 * Configuration field values
495 *
496 * @return array
497 */
498 public function getConfigFieldsValues()
499 {
500 return [
501 'MA_CUSTOMER_QTY' => Tools::getValue('MA_CUSTOMER_QTY', Configuration::get('MA_CUSTOMER_QTY')),
502 'MA_MERCHANT_ORDER' => Tools::getValue('MA_MERCHANT_ORDER', Configuration::get('MA_MERCHANT_ORDER')),
503 'MA_MERCHANT_OOS' => Tools::getValue('MA_MERCHANT_OOS', Configuration::get('MA_MERCHANT_OOS')),
504 'MA_LAST_QTIES' => Tools::getValue('MA_LAST_QTIES', Configuration::get('MA_LAST_QTIES')),
505 'MA_MERCHANT_COVERAGE' => Tools::getValue('MA_MERCHANT_COVERAGE', Configuration::get('MA_MERCHANT_COVERAGE')),
506 'MA_PRODUCT_COVERAGE' => Tools::getValue('MA_PRODUCT_COVERAGE', Configuration::get('MA_PRODUCT_COVERAGE')),
507 'MA_MERCHANT_MAILS' => Tools::getValue('MA_MERCHANT_MAILS', Configuration::get('MA_MERCHANT_MAILS')),
508 'MA_BCC' => Tools::getValue('MA_BCC', Configuration::get('MA_BCC')),
509 'MA_ORDER_EDIT' => Tools::getValue('MA_ORDER_EDIT', Configuration::get('MA_ORDER_EDIT')),
510 'MA_RETURN_SLIP' => Tools::getValue('MA_RETURN_SLIP', Configuration::get('MA_RETURN_SLIP')),
511 ];
512 }
513
514 public function hookActionValidateOrder($params)
515 {
516 if (!$this->merchant_order || empty($this->merchant_mails)) {
517 return;
518 }
519
520 // Getting differents vars
521 $context = Context::getContext();
522 $idLang = (int) $context->language->id;
523 $idShop = (int) $context->shop->id;
524 $currency = $params['currency'];
525 $order = $params['order'];
526 $customer = $params['customer'];
527 $configuration = Configuration::getMultiple(
528 [
529 'PS_SHOP_EMAIL',
530 'PS_MAIL_METHOD',
531 'PS_MAIL_SERVER',
532 'PS_MAIL_USER',
533 'PS_MAIL_PASSWD',
534 'PS_SHOP_NAME',
535 'PS_MAIL_COLOR',
536 ],
537 $idLang,
538 null,
539 $idShop
540 );
541 $delivery = new Address((int) $order->id_address_delivery);
542 $invoice = new Address((int) $order->id_address_invoice);
543 $orderDateText = Tools::displayDate($order->date_add);
544 $carrier = new Carrier((int) $order->id_carrier);
545 $message = $this->getAllMessages($order->id);
546
547 if (!$message || empty($message)) {
548 $message = $this->l('No message');
549 }
550
551 $itemsTable = '';
552
553 $products = $params['order']->getProducts();
554 $customizedDatas = Product::getAllCustomizedDatas((int) $params['cart']->id);
555 Product::addCustomizationPrice($products, $customizedDatas);
556 foreach ($products as $key => $product) {
557 $unitPrice = Product::getTaxCalculationMethod($customer->id) == PS_TAX_EXC ? $product['product_price'] : $product['product_price_wt'];
558
559 $customizationText = '';
560 if (isset($customizedDatas[$product['product_id']][$product['product_attribute_id']])) {
561 foreach ($customizedDatas[$product['product_id']][$product['product_attribute_id']][$order->id_address_delivery] as $customization) {
562 if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD])) {
563 foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text) {
564 $customizationText .= $text['name'].': '.$text['value'].'<br />';
565 }
566 }
567
568 if (isset($customization['datas'][Product::CUSTOMIZE_FILE])) {
569 $customizationText .= count($customization['datas'][Product::CUSTOMIZE_FILE]).' '.$this->l('image(s)').'<br />';
570 }
571
572 $customizationText .= '---<br />';
573 }
574 if (method_exists('Tools', 'rtrimString')) {
575 $customizationText = Tools::rtrimString($customizationText, '---<br />');
576 } else {
577 $customizationText = preg_replace('/---<br \/>$/', '', $customizationText);
578 }
579 }
580
581 $url = $context->link->getProductLink($product['product_id']);
582 $itemsTable .=
583 '<tr style="background-color:'.($key % 2 ? '#DDE2E6' : '#EBECEE').';">
584 <td style="padding:0.6em 0.4em;">'.$product['product_reference'].'</td>
585 <td style="padding:0.6em 0.4em;">
586 <strong><a href="'.$url.'">'.$product['product_name'].'</a>'
587 .(isset($product['attributes_small']) ? ' '.$product['attributes_small'] : '')
588 .(!empty($customizationText) ? '<br />'.$customizationText : '')
589 .'</strong>
590 </td>
591 <td style="padding:0.6em 0.4em; text-align:right;">'.Tools::displayPrice($unitPrice, $currency, false).'</td>
592 <td style="padding:0.6em 0.4em; text-align:center;">'.(int) $product['product_quantity'].'</td>
593 <td style="padding:0.6em 0.4em; text-align:right;">'
594 .Tools::displayPrice(($unitPrice * $product['product_quantity']), $currency, false)
595 .'</td>
596 </tr>';
597 }
598 foreach ($params['order']->getCartRules() as $discount) {
599 $itemsTable .=
600 '<tr style="background-color:#EBECEE;">
601 <td colspan="4" style="padding:0.6em 0.4em; text-align:right;">'.$this->l('Voucher code:').' '.$discount['name'].'</td>
602 <td style="padding:0.6em 0.4em; text-align:right;">-'.Tools::displayPrice($discount['value'], $currency, false).'</td>
603 </tr>';
604 }
605 if ($delivery->id_state) {
606 $deliveryState = new State((int) $delivery->id_state);
607 }
608 if ($invoice->id_state) {
609 $invoiceState = new State((int) $invoice->id_state);
610 }
611
612 /** @var Order $order */
613 if (Product::getTaxCalculationMethod($customer->id) == PS_TAX_EXC) {
614 $totalProducts = $order->getTotalProductsWithoutTaxes();
615 } else {
616 $totalProducts = $order->getTotalProductsWithTaxes();
617 }
618
619 $orderState = $params['orderStatus'];
620
621 // Filling-in vars for email
622 $templateVars = [
623 '{firstname}' => $customer->firstname,
624 '{lastname}' => $customer->lastname,
625 '{email}' => $customer->email,
626 '{delivery_block_txt}' => MailAlert::getFormatedAddress($delivery, "\n"),
627 '{invoice_block_txt}' => MailAlert::getFormatedAddress($invoice, "\n"),
628 '{delivery_block_html}' => MailAlert::getFormatedAddress(
629 $delivery, '<br />', [
630 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
631 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
632 ]
633 ),
634 '{invoice_block_html}' => MailAlert::getFormatedAddress(
635 $invoice, '<br />', [
636 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
637 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
638 ]
639 ),
640 '{delivery_company}' => $delivery->company,
641 '{delivery_firstname}' => $delivery->firstname,
642 '{delivery_lastname}' => $delivery->lastname,
643 '{delivery_address1}' => $delivery->address1,
644 '{delivery_address2}' => $delivery->address2,
645 '{delivery_city}' => $delivery->city,
646 '{delivery_postal_code}' => $delivery->postcode,
647 '{delivery_country}' => $delivery->country,
648 '{delivery_state}' => $delivery->id_state ? $deliveryState->name : '',
649 '{delivery_phone}' => $delivery->phone ? $delivery->phone : $delivery->phone_mobile,
650 '{delivery_other}' => $delivery->other,
651 '{invoice_company}' => $invoice->company,
652 '{invoice_firstname}' => $invoice->firstname,
653 '{invoice_lastname}' => $invoice->lastname,
654 '{invoice_address2}' => $invoice->address2,
655 '{invoice_address1}' => $invoice->address1,
656 '{invoice_city}' => $invoice->city,
657 '{invoice_postal_code}' => $invoice->postcode,
658 '{invoice_country}' => $invoice->country,
659 '{invoice_state}' => $invoice->id_state ? $invoiceState->name : '',
660 '{invoice_phone}' => $invoice->phone ? $invoice->phone : $invoice->phone_mobile,
661 '{invoice_other}' => $invoice->other,
662 '{order_name}' => $order->reference,
663 '{order_status}' => $orderState->name,
664 '{shop_name}' => $configuration['PS_SHOP_NAME'],
665 '{date}' => $orderDateText,
666 '{carrier}' => (($carrier->name == '0') ? $configuration['PS_SHOP_NAME'] : $carrier->name),
667 '{payment}' => Tools::substr($order->payment, 0, 32),
668 '{items}' => $itemsTable,
669 '{total_paid}' => Tools::displayPrice($order->total_paid, $currency),
670 '{total_products}' => Tools::displayPrice($totalProducts, $currency),
671 '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
672 '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
673 '{total_tax_paid}' => Tools::displayPrice(
674 ($order->total_products_wt - $order->total_products) + ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl),
675 $currency,
676 false
677 ),
678 '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
679 '{currency}' => $currency->sign,
680 '{gift}' => (bool) $order->gift,
681 '{gift_message}' => $order->gift_message,
682 '{message}' => $message,
683 ];
684
685 // Shop iso
686 $iso = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT'));
687
688 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
689 $merchantMails = explode(static::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
690 foreach ($merchantMails as $merchantMail) {
691 // Default language
692 $mailIdLang = $idLang;
693 $mailIso = $iso;
694
695 // Use the merchant lang if he exists as an employee
696 $results = Db::getInstance()->executeS(
697 '
698 SELECT `id_lang` FROM `'._DB_PREFIX_.'employee`
699 WHERE `email` = \''.pSQL($merchantMail).'\'
700 '
701 );
702 if ($results) {
703 $userIso = Language::getIsoById((int) $results[0]['id_lang']);
704 if ($userIso) {
705 $mailIdLang = (int) $results[0]['id_lang'];
706 $mailIso = $userIso;
707 }
708 }
709
710 $dirMail = false;
711 if (file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/new_order.txt") &&
712 file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/new_order.html")) {
713 $dirMail = _PS_THEME_DIR_."modules/$this->name/mails/";
714 } elseif (file_exists(__DIR__."/mails/$mailIso/new_order.txt") &&
715 file_exists(__DIR__."/mails/$mailIso/new_order.html")
716 ) {
717 $dirMail = __DIR__.'/mails/';
718 } elseif (file_exists(_PS_MAIL_DIR_.$mailIso.'/new_order.txt') &&
719 file_exists(_PS_MAIL_DIR_.$mailIso.'/new_order.html')) {
720 $dirMail = _PS_MAIL_DIR_;
721 } elseif (Language::getIdByIso('en')) {
722 $mailIdLang = (int) Language::getIdByIso('en');
723 $dirMail = __DIR__.'/mails/';
724 }
725
726 if ($dirMail) {
727 Mail::Send(
728 $mailIdLang,
729 'new_order',
730 sprintf(Mail::l('New order : #%d - %s', $mailIdLang), $order->id, $order->reference),
731 $templateVars,
732 $merchantMail,
733 $bcc = array($this->bcc_mails),
734 $configuration['PS_SHOP_EMAIL'],
735 $configuration['PS_SHOP_NAME'],
736 null,
737 null,
738 $dirMail,
739 null,
740 $idShop
741 );
742 }
743 }
744 }
745
746 /**
747 * Get all messages
748 *
749 * @param int $id
750 *
751 * @return string
752 */
753 public function getAllMessages($id)
754 {
755 $messages = Db::getInstance()->executeS(
756 '
757 SELECT `message`
758 FROM `'._DB_PREFIX_.'message`
759 WHERE `id_order` = '.(int) $id.'
760 ORDER BY `id_message` ASC'
761 );
762 $result = [];
763 foreach ($messages as $message) {
764 $result[] = $message['message'];
765 }
766
767 return implode('<br/>', $result);
768 }
769
770 /**
771 *
772 *
773 * @param array $params
774 *
775 * @return string
776 */
777 public function hookActionProductOutOfStock($params)
778 {
779 if (!$this->customer_qty ||
780 !Configuration::get('PS_STOCK_MANAGEMENT') ||
781 Product::isAvailableWhenOutOfStock($params['product']->out_of_stock)
782 ) {
783 return '';
784 }
785
786 $context = Context::getContext();
787 $idProduct = (int) $params['product']->id;
788 $idProductAttribute = 0;
789 $idCustomer = (int) $context->customer->id;
790
791 if ((int) $context->customer->id <= 0) {
792 $this->context->smarty->assign('email', 1);
793 } elseif (MailAlert::customerHasNotification($idCustomer, $idProduct, $idProductAttribute, (int) $context->shop->id)) {
794 return '';
795 }
796
797 $this->context->smarty->assign(
798 [
799 'id_product' => $idProduct,
800 'id_product_attribute' => $idProductAttribute,
801 ]
802 );
803
804 return $this->display(__FILE__, 'product.tpl');
805 }
806
807 /**
808 * @param array $params
809 */
810 public function hookActionUpdateQuantity($params)
811 {
812 $idProduct = (int) $params['id_product'];
813 $idProductAttribute = (int) $params['id_product_attribute'];
814
815 $quantity = (int) $params['quantity'];
816 $context = Context::getContext();
817 $idShop = (int) $context->shop->id;
818 $idLang = (int) $context->language->id;
819 $product = new Product($idProduct, false, $idLang, $idShop, $context);
820 $productHasAttributes = $product->hasAttributes();
821 $configuration = Configuration::getMultiple(
822 [
823 'MA_LAST_QTIES',
824 'PS_STOCK_MANAGEMENT',
825 'PS_SHOP_EMAIL',
826 'PS_SHOP_NAME',
827 ],
828 null,
829 null,
830 $idShop
831 );
832 $maLastQties = (int) $configuration['MA_LAST_QTIES'];
833
834 $checkOos = ($productHasAttributes && $idProductAttribute) || (!$productHasAttributes && !$idProductAttribute);
835
836 if ($checkOos &&
837 $product->active == 1 &&
838 (int) $quantity <= $maLastQties &&
839 !(!$this->merchant_oos || empty($this->merchant_mails)) &&
840 $configuration['PS_STOCK_MANAGEMENT']
841 ) {
842 $mailIso = Language::getIsoById($idLang);
843 $productName = Product::getProductName($idProduct, $idProductAttribute, $idLang);
844 $templateVars = [
845 '{qty}' => $quantity,
846 '{last_qty}' => $maLastQties,
847 '{product}' => $productName,
848 ];
849
850 $dirMail = false;
851 if (file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/productoutofstock.txt") &&
852 file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/productoutofstock.html")) {
853 $dirMail = _PS_THEME_DIR_."modules/$this->name/mails/";
854 } elseif (file_exists(__DIR__."/mails/$mailIso/productoutofstock.txt") &&
855 file_exists(__DIR__."/mails/$mailIso/productoutofstock.html")
856 ) {
857 $dirMail = __DIR__.'/mails/';
858 } elseif (file_exists(_PS_MAIL_DIR_.$mailIso.'/productoutofstock.txt') &&
859 file_exists(_PS_MAIL_DIR_.$mailIso.'/productoutofstock.html')
860 ) {
861 $dirMail = _PS_MAIL_DIR_;
862 } elseif (Language::getIdByIso('en')) {
863 $dirMail = __DIR__."/mails/";
864 $idLang = (int) Language::getIdByIso('en');
865 }
866
867 // Do not send mail if multiples product are created / imported.
868 if (!defined('PS_MASS_PRODUCT_CREATION') && $dirMail) {
869 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
870 $merchantMails = explode(static::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
871 foreach ($merchantMails as $merchantMail) {
872 Mail::Send(
873 $idLang,
874 'productoutofstock',
875 Mail::l('Product out of stock', $idLang),
876 $templateVars,
877 $merchantMail,
878 null,
879 (string) $configuration['PS_SHOP_EMAIL'],
880 (string) $configuration['PS_SHOP_NAME'],
881 null,
882 null,
883 $dirMail,
884 false,
885 $idShop
886 );
887 }
888 }
889 }
890
891 if ($this->customer_qty && $quantity > 0) {
892 MailAlert::sendCustomerAlert((int) $product->id, (int) $params['id_product_attribute']);
893 }
894 }
895
896 /**
897 * @param array $params
898 */
899 public function hookActionProductAttributeUpdate($params)
900 {
901 $sql = '
902 SELECT `id_product`, `quantity`
903 FROM `'._DB_PREFIX_.'stock_available`
904 WHERE `id_product_attribute` = '.(int) $params['id_product_attribute'];
905
906 $result = Db::getInstance()->getRow($sql);
907
908 if ($this->customer_qty && $result['quantity'] > 0) {
909 MailAlert::sendCustomerAlert((int) $result['id_product'], (int) $params['id_product_attribute']);
910 }
911 }
912
913 /**
914 * @param array $params
915 *
916 * @return string
917 */
918 public function hookDisplayMyAccountBlock($params)
919 {
920 return $this->hookDisplayCustomerAccount($params);
921 }
922
923 /**
924 * @return string
925 */
926 public function hookDisplayCustomerAccount()
927 {
928 return $this->customer_qty ? $this->display(__FILE__, 'my-account.tpl') : '';
929 }
930
931 /**
932 * @param array $params
933 */
934 public function hookActionProductDelete($params)
935 {
936 $sql = '
937 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
938 WHERE `id_product` = '.(int) $params['product']->id;
939
940 Db::getInstance()->execute($sql);
941 }
942
943 /**
944 * @param array $params
945 */
946 public function hookActionAttributeDelete($params)
947 {
948 if ($params['deleteAllAttributes']) {
949 $sql = '
950 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
951 WHERE `id_product` = '.(int) $params['id_product'];
952 } else {
953 $sql = '
954 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
955 WHERE `id_product_attribute` = '.(int) $params['id_product_attribute'].'
956 AND `id_product` = '.(int) $params['id_product'];
957 }
958
959 Db::getInstance()->execute($sql);
960 }
961
962 /**
963 * @param array $params
964 */
965 public function hookActionProductCoverage($params)
966 {
967 // if not advanced stock management, nothing to do
968 if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
969 return;
970 }
971
972 // retrieves informations
973 $idProduct = (int) $params['id_product'];
974 $idProductAttribute = (int) $params['id_product_attribute'];
975 $warehouse = $params['warehouse'];
976 $product = new Product($idProduct);
977
978 if (!Validate::isLoadedObject($product)) {
979 return;
980 }
981
982 if (!$product->advanced_stock_management) {
983 return;
984 }
985
986 // sets warehouse id to get the coverage
987 if (!Validate::isLoadedObject($warehouse)) {
988 $idWarehouse = 0;
989 } else {
990 $idWarehouse = (int) $warehouse->id;
991 }
992
993 // coverage of the product
994 $warningCoverage = (int) Configuration::getGlobalValue('MA_PRODUCT_COVERAGE');
995
996 $coverage = StockManagerFactory::getManager()->getProductCoverage($idProduct, $idProductAttribute, $warningCoverage, $idWarehouse);
997
998 // if we need to send a notification
999 if ($product->active == 1 &&
1000 ($coverage < $warningCoverage) && !empty($this->merchant_mails) &&
1001 Configuration::getGlobalValue('MA_MERCHANT_COVERAGE')
1002 ) {
1003 $context = Context::getContext();
1004 $idLang = (int) $context->language->id;
1005 $idShop = (int) $context->shop->id;
1006 $mailIso = Language::getIsoById($idLang);
1007 $productName = Product::getProductName($idProduct, $idProductAttribute, $idLang);
1008 $templateVars = [
1009 '{current_coverage}' => $coverage,
1010 '{warning_coverage}' => $warningCoverage,
1011 '{product}' => pSQL($productName),
1012 ];
1013
1014 $dirMail = false;
1015 if (file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/productcoverage.txt") &&
1016 file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/productcoverage.html")) {
1017 $dirMail = _PS_THEME_DIR_."modules/$this->name/mails/";
1018 } elseif (file_exists(__DIR__."/mails/$mailIso/productcoverage.txt") &&
1019 file_exists(__DIR__."/mails/$mailIso/productcoverage.html")
1020 ) {
1021 $dirMail = __DIR__.'/mails/';
1022 } elseif (file_exists(_PS_MAIL_DIR_.$mailIso.'/productcoverage.txt') &&
1023 file_exists(_PS_MAIL_DIR_.$mailIso.'/productcoverage.html')
1024 ) {
1025 $dirMail = _PS_MAIL_DIR_;
1026 } elseif (Language::getIdByIso('en')) {
1027 $idLang = (int) Language::getIdByIso('en');
1028 $dirMail = __DIR__.'/mails/';
1029 }
1030
1031 if ($dirMail) {
1032 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
1033 $merchantMails = explode(static::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
1034 foreach ($merchantMails as $merchantMail) {
1035 Mail::Send(
1036 $idLang,
1037 'productcoverage',
1038 Mail::l('Stock coverage', $idLang),
1039 $templateVars,
1040 $merchantMail,
1041 null,
1042 (string) Configuration::get('PS_SHOP_EMAIL'),
1043 (string) Configuration::get('PS_SHOP_NAME'),
1044 null,
1045 null,
1046 $dirMail,
1047 null,
1048 $idShop
1049 );
1050 }
1051 }
1052 }
1053 }
1054
1055 public function hookDisplayHeader()
1056 {
1057 $this->page_name = Dispatcher::getInstance()->getController();
1058 if (in_array($this->page_name, ['product', 'account'])) {
1059 $this->context->controller->addJS($this->_path.'js/mailalerts.js');
1060 $this->context->controller->addCSS($this->_path.'css/mailalerts.css', 'all');
1061 }
1062 }
1063
1064 /**
1065 * Send a mail when a customer return an order.
1066 *
1067 * @param array $params Hook params.
1068 */
1069 public function hookActionOrderReturn($params)
1070 {
1071 if (!$this->return_slip || empty($this->return_slip)) {
1072 return;
1073 }
1074
1075 $context = Context::getContext();
1076 $idLang = (int) $context->language->id;
1077 $idShop = (int) $context->shop->id;
1078 $configuration = Configuration::getMultiple(
1079 [
1080 'PS_SHOP_EMAIL',
1081 'PS_MAIL_METHOD',
1082 'PS_MAIL_SERVER',
1083 'PS_MAIL_USER',
1084 'PS_MAIL_PASSWD',
1085 'PS_SHOP_NAME',
1086 'PS_MAIL_COLOR',
1087 ],
1088 $idLang,
1089 null,
1090 $idShop
1091 );
1092
1093 // Shop iso
1094 $iso = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT'));
1095
1096 $order = new Order((int) $params['orderReturn']->id_order);
1097 $customer = new Customer((int) $params['orderReturn']->id_customer);
1098 $delivery = new Address((int) $order->id_address_delivery);
1099 $invoice = new Address((int) $order->id_address_invoice);
1100 $orderDateText = Tools::displayDate($order->date_add);
1101 if ($delivery->id_state) {
1102 $deliveryState = new State((int) $delivery->id_state);
1103 }
1104 if ($invoice->id_state) {
1105 $invoiceState = new State((int) $invoice->id_state);
1106 }
1107
1108 $orderReturnProducts = OrderReturn::getOrdersReturnProducts($params['orderReturn']->id, $order);
1109
1110 $itemsTable = '';
1111 foreach ($orderReturnProducts as $key => $product) {
1112 $url = $context->link->getProductLink($product['product_id']);
1113 $itemsTable .=
1114 '<tr style="background-color:'.($key % 2 ? '#DDE2E6' : '#EBECEE').';">
1115 <td style="padding:0.6em 0.4em;">'.$product['product_reference'].'</td>
1116 <td style="padding:0.6em 0.4em;">
1117 <strong><a href="'.$url.'">'.$product['product_name'].'</a>
1118 </strong>
1119 </td>
1120 <td style="padding:0.6em 0.4em; text-align:center;">'.(int) $product['product_quantity'].'</td>
1121 </tr>';
1122 }
1123
1124 $templateVars = [
1125 '{firstname}' => $customer->firstname,
1126 '{lastname}' => $customer->lastname,
1127 '{email}' => $customer->email,
1128 '{delivery_block_txt}' => MailAlert::getFormatedAddress($delivery, "\n"),
1129 '{invoice_block_txt}' => MailAlert::getFormatedAddress($invoice, "\n"),
1130 '{delivery_block_html}' => MailAlert::getFormatedAddress(
1131 $delivery, '<br />', [
1132 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
1133 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
1134 ]
1135 ),
1136 '{invoice_block_html}' => MailAlert::getFormatedAddress(
1137 $invoice, '<br />', [
1138 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
1139 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
1140 ]
1141 ),
1142 '{delivery_company}' => $delivery->company,
1143 '{delivery_firstname}' => $delivery->firstname,
1144 '{delivery_lastname}' => $delivery->lastname,
1145 '{delivery_address1}' => $delivery->address1,
1146 '{delivery_address2}' => $delivery->address2,
1147 '{delivery_city}' => $delivery->city,
1148 '{delivery_postal_code}' => $delivery->postcode,
1149 '{delivery_country}' => $delivery->country,
1150 '{delivery_state}' => $delivery->id_state ? $deliveryState->name : '',
1151 '{delivery_phone}' => $delivery->phone ? $delivery->phone : $delivery->phone_mobile,
1152 '{delivery_other}' => $delivery->other,
1153 '{invoice_company}' => $invoice->company,
1154 '{invoice_firstname}' => $invoice->firstname,
1155 '{invoice_lastname}' => $invoice->lastname,
1156 '{invoice_address2}' => $invoice->address2,
1157 '{invoice_address1}' => $invoice->address1,
1158 '{invoice_city}' => $invoice->city,
1159 '{invoice_postal_code}' => $invoice->postcode,
1160 '{invoice_country}' => $invoice->country,
1161 '{invoice_state}' => $invoice->id_state ? $invoiceState->name : '',
1162 '{invoice_phone}' => $invoice->phone ? $invoice->phone : $invoice->phone_mobile,
1163 '{invoice_other}' => $invoice->other,
1164 '{order_name}' => $order->reference,
1165 '{shop_name}' => $configuration['PS_SHOP_NAME'],
1166 '{date}' => $orderDateText,
1167 '{items}' => $itemsTable,
1168 '{message}' => Tools::purifyHTML($params['orderReturn']->question),
1169 ];
1170
1171 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
1172 $merchantMails = explode(static::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
1173 foreach ($merchantMails as $merchantMail) {
1174 // Default language
1175 $mailIdLang = $idLang;
1176 $mailIso = $iso;
1177
1178 // Use the merchant lang if he exists as an employee
1179 $results = Db::getInstance()->executeS(
1180 '
1181 SELECT `id_lang` FROM `'._DB_PREFIX_.'employee`
1182 WHERE `email` = \''.pSQL($merchantMail).'\'
1183 '
1184 );
1185 if ($results) {
1186 $userIso = Language::getIsoById((int) $results[0]['id_lang']);
1187 if ($userIso) {
1188 $mailIdLang = (int) $results[0]['id_lang'];
1189 $mailIso = $userIso;
1190 }
1191 }
1192
1193 $dirMail = false;
1194 if (file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/return_slip.txt") &&
1195 file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/return_slip.html")) {
1196 $dirMail = _PS_THEME_DIR_."modules/$this->name/mails/";
1197 } elseif (file_exists(__DIR__."/mails/$mailIso/return_slip.txt") &&
1198 file_exists(__DIR__."/mails/$mailIso/return_slip.html")
1199 ) {
1200 $dirMail = __DIR__.'/mails/';
1201 } elseif (file_exists(_PS_MAIL_DIR_.$mailIso.'/return_slip.txt') &&
1202 file_exists(_PS_MAIL_DIR_.$mailIso.'/return_slip.html')
1203 ) {
1204 $dirMail = _PS_MAIL_DIR_;
1205 } elseif (Language::getIdByIso('en')) {
1206 $mailIdLang = (int) Language::getIdByIso('en');
1207 $dirMail = __DIR__.'/mails/';
1208 }
1209
1210 if ($dirMail) {
1211 Mail::Send(
1212 $mailIdLang,
1213 'return_slip',
1214 sprintf(Mail::l('New return from order #%d - %s', $mailIdLang), $order->id, $order->reference),
1215 $templateVars,
1216 $merchantMail,
1217 null,
1218 $configuration['PS_SHOP_EMAIL'],
1219 $configuration['PS_SHOP_NAME'],
1220 null,
1221 null,
1222 $dirMail,
1223 null,
1224 $idShop
1225 );
1226 }
1227 }
1228 }
1229
1230 /**
1231 * Send a mail when an order is modified.
1232 *
1233 * @param array $params Hook params.
1234 */
1235 public function hookActionOrderEdited($params)
1236 {
1237 if (!$this->order_edited || empty($this->order_edited)) {
1238 return;
1239 }
1240
1241 $order = $params['order'];
1242
1243 $data = [
1244 '{lastname}' => $order->getCustomer()->lastname,
1245 '{firstname}' => $order->getCustomer()->firstname,
1246 '{id_order}' => (int) $order->id,
1247 '{order_name}' => $order->getUniqReference(),
1248 ];
1249
1250 $language = new Language((int) $order->id_lang);
1251 if (Validate::isLoadedObject($language)) {
1252 $mailIso = $language->iso_code;
1253 } else {
1254 $mailIso = Context::getContext()->language->iso_code;
1255 }
1256
1257 $dirMail = false;
1258 if (file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/order_changed.txt") &&
1259 file_exists(_PS_THEME_DIR_."modules/$this->name/mails/$mailIso/order_changed.html")) {
1260 $dirMail = _PS_THEME_DIR_."modules/$this->name/mails/";
1261 } elseif (file_exists(__DIR__."/mails/$mailIso/order_changed.txt") &&
1262 file_exists(__DIR__."/mails/$mailIso/order_changed.html")
1263 ) {
1264 $dirMail = __DIR__.'/mails/';
1265 } elseif (file_exists(_PS_MAIL_DIR_.$mailIso.'/order_changed.txt') &&
1266 file_exists(_PS_MAIL_DIR_.$mailIso.'/order_changed.html')
1267 ) {
1268 $dirMail = _PS_MAIL_DIR_;
1269 } elseif (Language::getIdByIso('en')) {
1270 $mailIso = 'en';
1271 $dirMail = __DIR__.'/mails/';
1272 }
1273
1274 Mail::Send(
1275 Language::getIdByIso($mailIso),
1276 'order_changed',
1277 Mail::l(
1278 'Your order has been changed',
1279 (int) $order->id_lang
1280 ),
1281 $data,
1282 $order->getCustomer()->email,
1283 $order->getCustomer()->firstname.' '.$order->getCustomer()->lastname,
1284 null,
1285 null,
1286 null,
1287 null,
1288 $dirMail,
1289 true,
1290 (int) $order->id_shop
1291 );
1292 }
1293}