Add CSS hook, URL ref prefill, order prefill, and Widerrufsfrist display

- Fix CSS loading: register stylesheet via actionFrontControllerSetMedia
  hook instead of initContent() — guaranteed to fire before page render
- Upgrade script registers new hook on existing installations
- Feature 1: pre-fill order_reference from ?ref= URL parameter
- Feature 2: pre-fill customer name/email from logged-in account when
  navigating with ?ref= (already worked; now order ref is also pre-filled)
- Feature 4: compute Widerrufsfrist (order date +14 days) from DB and
  display on form (when ref in URL), confirm, and success pages
- Feature 3: EN mail templates were already present

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Arne Weiss
2026-06-01 08:52:53 +02:00
parent c4b104108a
commit 058937bd64
7 changed files with 128 additions and 7 deletions
+23 -7
View File
@@ -26,12 +26,6 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo
{
parent::initContent();
$this->registerStylesheet(
'cyp-withdrawal',
'modules/' . $this->module->name . '/views/css/withdrawal.css',
['media' => 'all', 'priority' => 200]
);
$this->context->smarty->assign([
'errors_list' => $this->errorsList,
'form_data' => $this->formData,
@@ -40,6 +34,7 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo
'success_data' => $this->successData,
'privacy_url' => (string) Configuration::get(Simple_withdrawalbutton::CONF_PRIVACY_URL),
'revocation_url' => (string) Configuration::get(Simple_withdrawalbutton::CONF_REVOCATION_URL),
'withdrawal_deadline' => $this->computeDeadline(),
]);
if ($this->currentView === 'confirm') {
@@ -185,16 +180,37 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo
$customerEmail = (string) $this->context->customer->email;
}
$orderRef = $this->module->cleanText(Tools::getValue('ref'), 64);
return [
'customer_name' => $customerName,
'customer_email' => $customerEmail,
'order_reference' => '',
'order_reference' => $orderRef,
'withdrawal_scope' => 'full',
'withdrawal_items_text' => '',
'message' => '',
];
}
private function computeDeadline()
{
$ref = isset($this->formData['order_reference']) ? $this->formData['order_reference'] : '';
if ($ref === '') {
$ref = $this->module->cleanText(Tools::getValue('ref'), 64);
}
if ($ref === '') {
return '';
}
$orderInfo = $this->module->lookupOrderInfoByReference($ref);
if (!$orderInfo) {
return '';
}
$isoCode = Language::getIsoById((int) $this->context->language->id) ?: 'de';
return (string) $this->module->computeWithdrawalDeadline($orderInfo['date_add'], $isoCode);
}
private function validateData(array $data)
{
$errors = [];
+52
View File
@@ -50,6 +50,7 @@ class Simple_withdrawalbutton extends Module
&& $this->installTab()
&& $this->registerHook('displayFooter')
&& $this->registerHook('displayCustomerAccount')
&& $this->registerHook('actionFrontControllerSetMedia')
&& Configuration::updateValue(self::CONF_SHOP_EMAIL, (string) Configuration::get('PS_SHOP_EMAIL'))
&& Configuration::updateValue(self::CONF_RATE_LIMIT, '5')
&& Configuration::updateValue(self::CONF_PRIVACY_URL, '')
@@ -295,6 +296,20 @@ class Simple_withdrawalbutton extends Module
return $this->display(__FILE__, 'views/templates/hook/customer_account.tpl');
}
public function hookActionFrontControllerSetMedia()
{
if (
Tools::getValue('module') === $this->name
&& Tools::getValue('controller') === 'withdraw'
) {
$this->context->controller->registerStylesheet(
'cyp-withdrawal',
'modules/' . $this->name . '/views/css/withdrawal.css',
['media' => 'all', 'priority' => 200]
);
}
}
public function getWithdrawalLink()
{
return $this->context->link->getModuleLink($this->name, 'withdraw', [], true);
@@ -342,6 +357,43 @@ class Simple_withdrawalbutton extends Module
return $idOrder > 0 ? $idOrder : null;
}
public function lookupOrderInfoByReference($orderReference)
{
$orderReference = $this->cleanText($orderReference, 64);
if ($orderReference === '') {
return null;
}
$sql = 'SELECT `id_order`, `date_add`, `id_customer`
FROM `' . _DB_PREFIX_ . 'orders`
WHERE `reference` = "' . pSQL($orderReference) . '"
AND `id_shop` = ' . (int) $this->context->shop->id . '
ORDER BY `id_order` DESC';
$row = Db::getInstance()->getRow($sql);
if (!$row) {
return null;
}
return [
'id_order' => (int) $row['id_order'],
'date_add' => $row['date_add'],
'id_customer' => (int) $row['id_customer'],
];
}
public function computeWithdrawalDeadline($orderDateAdd, $isoCode = 'de')
{
$timestamp = strtotime((string) $orderDateAdd);
if (!$timestamp) {
return null;
}
$deadline = strtotime('+14 days', $timestamp);
return ($isoCode === 'de') ? date('d.m.Y', $deadline) : date('d/m/Y', $deadline);
}
public function hashForPrivacy($value)
{
$value = trim((string) $value);
+8
View File
@@ -0,0 +1,8 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
+10
View File
@@ -0,0 +1,10 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
function upgrade_module_0_1_5($object)
{
return $object->registerHook('actionFrontControllerSetMedia');
}
+11
View File
@@ -70,6 +70,17 @@
<div class="cyp-summary-val">{$form_data.message|escape:'html':'UTF-8'|nl2br nofilter}</div>
</div>
{/if}
{if isset($withdrawal_deadline) && $withdrawal_deadline != ''}
<div class="cyp-summary-row">
<div class="cyp-summary-key">{l s='Frist' mod='simple_withdrawalbutton'}</div>
<div class="cyp-summary-val" style="color:var(--cyw-accent);font-weight:600;">
{l s='bis' mod='simple_withdrawalbutton'} {$withdrawal_deadline|escape:'html':'UTF-8'}
<span style="font-size:12px;font-weight:400;color:var(--cyw-text-muted);">
({l s='14 Tage ab Bestelldatum' mod='simple_withdrawalbutton'})
</span>
</div>
</div>
{/if}
</div>
<form method="post" action="{$action_url|escape:'html':'UTF-8'}">
+13
View File
@@ -28,6 +28,19 @@
{/if}
</div>
{if isset($withdrawal_deadline) && $withdrawal_deadline != ''}
<div class="cyp-deadline-notice" style="
display:flex; align-items:center; gap:10px; background:#eef2f8;
border:1px solid #c5d4eb; border-radius:6px; padding:11px 14px;
font-size:13px; color:var(--cyw-accent); margin-bottom:24px;">
<span style="font-size:16px;">📅</span>
<span>
{l s='Widerrufsfrist (14 Tage ab Bestelldatum): bis' mod='simple_withdrawalbutton'}
<strong>{$withdrawal_deadline|escape:'html':'UTF-8'}</strong>
</span>
</div>
{/if}
{if $errors_list|@count > 0}
<div class="alert alert-danger" role="alert">
<ul>
+11
View File
@@ -77,6 +77,17 @@
<div class="cyp-summary-val">{$success_data.message|escape:'html':'UTF-8'|nl2br nofilter}</div>
</div>
{/if}
{if isset($withdrawal_deadline) && $withdrawal_deadline != ''}
<div class="cyp-summary-row">
<div class="cyp-summary-key">{l s='Frist' mod='simple_withdrawalbutton'}</div>
<div class="cyp-summary-val" style="color:var(--cyw-accent);font-weight:600;">
{l s='bis' mod='simple_withdrawalbutton'} {$withdrawal_deadline|escape:'html':'UTF-8'}
<span style="font-size:12px;font-weight:400;color:var(--cyw-text-muted);">
({l s='14 Tage ab Bestelldatum' mod='simple_withdrawalbutton'})
</span>
</div>
</div>
{/if}
</div>
<div class="cyp-legal">