Skip to content

Commit 4f6fe24

Browse files
committed
PES-3058: Information about the selected pickup point to emails for e-shop owner. (squashed)
1 parent 5b24dd6 commit 4f6fe24

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed

packetery/CHANGE_LOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
v3.4
3+
- Added: Information about the selected pickup point to emails for e-shop owner.
14
- Updated: Loading the widget javascript library only on the checkout page.
25
- Updated: Multiple issues form PrestaShop validator resolved.
36
- Updated: Added company to shipment submission via API.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

packetery/packetery.php

100644100755
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,27 +1383,9 @@ public function hookDisplayOrderDetail($params)
13831383
*/
13841384
public function hookSendMailAlterTemplateVars(&$params)
13851385
{
1386-
if (
1387-
!isset(
1388-
$params['template'],
1389-
$params['template_vars']['{id_order}'],
1390-
$params['template_vars']['{carrier}']
1391-
)
1392-
|| strpos((string) $params['template'], 'order') === false
1393-
) {
1394-
return;
1395-
}
1396-
1397-
$orderRepository = $this->diContainer->get(Packetery\Order\OrderRepository::class);
1398-
$orderData = $orderRepository->getById((int) $params['template_vars']['{id_order}']);
1399-
if (!$orderData) {
1400-
return;
1401-
}
1402-
1403-
$params['template_vars']['{carrier}'] .= ' - ' . $orderData['name_branch'];
1404-
if ((bool) $orderData['is_carrier'] === false) {
1405-
$params['template_vars']['{carrier}'] .= sprintf(' (%s)', $orderData['id_branch']);
1406-
}
1386+
/** @var Packetery\Hooks\SendMailAlterTemplateVars $sendMailAlterTemplateVars */
1387+
$sendMailAlterTemplateVars = $this->diContainer->get(Packetery\Hooks\SendMailAlterTemplateVars::class);
1388+
$sendMailAlterTemplateVars->execute($params);
14071389
}
14081390

14091391
/**

0 commit comments

Comments
 (0)