|
| 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 | +} |
0 commit comments