|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Packeta s.r.o. <e-commerce.support@packeta.com> |
| 4 | + * @copyright 2015-2026 Packeta s.r.o. |
| 5 | + * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Packetery\Hooks; |
| 9 | + |
| 10 | +if (!defined('_PS_VERSION_')) { |
| 11 | + exit; |
| 12 | +} |
| 13 | + |
| 14 | +use Packetery\Exceptions\DatabaseException; |
| 15 | +use Packetery\Order\OrderRepository; |
| 16 | + |
| 17 | +class SendMailAlterTemplateVars |
| 18 | +{ |
| 19 | + /** @var OrderRepository */ |
| 20 | + private $orderRepository; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + OrderRepository $orderRepository |
| 24 | + ) { |
| 25 | + $this->orderRepository = $orderRepository; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Alters variables of order e-mails, append carrier with additional data |
| 30 | + * inspiration: https://github.com/PrestaShop/ps_legalcompliance/blob/dev/ps_legalcompliance.php |
| 31 | + * |
| 32 | + * @throws DatabaseException |
| 33 | + */ |
| 34 | + public function execute(array &$params): void |
| 35 | + { |
| 36 | + if ( |
| 37 | + $this->isOrderPage($params) === false |
| 38 | + || $this->hasCarrier($params) === false |
| 39 | + || $this->hasOrderOrCart($params) === false |
| 40 | + ) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $orderData = $this->getOrderData($params); |
| 45 | + if ($orderData !== null) { |
| 46 | + $additionalCarrierData = $this->getAdditionalCarrierInfo($orderData); |
| 47 | + $params['template_vars']['{carrier}'] .= $additionalCarrierData; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private function isOrderPage(array $params): bool |
| 52 | + { |
| 53 | + return isset($params['template']) && str_contains((string) $params['template'], 'order'); |
| 54 | + } |
| 55 | + |
| 56 | + private function hasCarrier(array $params): bool |
| 57 | + { |
| 58 | + return isset($params['template_vars']['{carrier}']) && is_string($params['template_vars']['{carrier}']); |
| 59 | + } |
| 60 | + |
| 61 | + private function hasOrderOrCart(array $params): bool |
| 62 | + { |
| 63 | + return isset($params['template_vars']['{id_order}']) || $this->hasCart($params); |
| 64 | + } |
| 65 | + |
| 66 | + private function hasCart(array $params): bool |
| 67 | + { |
| 68 | + return isset($params['cart']) && $params['cart'] instanceof \Cart; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @throws DatabaseException |
| 73 | + */ |
| 74 | + private function getOrderData(array $params): ?array |
| 75 | + { |
| 76 | + $orderData = null; |
| 77 | + if (isset($params['template_vars']['{id_order}'])) { |
| 78 | + $orderData = $this->orderRepository->getById((int) $params['template_vars']['{id_order}']); |
| 79 | + } elseif (isset($params['cart']->id)) { |
| 80 | + $orderData = $this->orderRepository->getByCart((int) $params['cart']->id); |
| 81 | + } |
| 82 | + |
| 83 | + if (!is_array($orderData)) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + return $orderData; |
| 88 | + } |
| 89 | + |
| 90 | + private function getAdditionalCarrierInfo(array $orderData): string |
| 91 | + { |
| 92 | + $carrierText = ' - ' . htmlspecialchars($orderData['name_branch'] ?? '', ENT_QUOTES); |
| 93 | + if (isset($orderData['is_carrier']) && (bool) $orderData['is_carrier'] === false) { |
| 94 | + $carrierText .= sprintf(' (%s)', (int) ($orderData['id_branch'] ?? '')); |
| 95 | + } |
| 96 | + |
| 97 | + return $carrierText; |
| 98 | + } |
| 99 | +} |
0 commit comments