From 058937bd64c8b5a90b29bce8f2cef6d246548490 Mon Sep 17 00:00:00 2001 From: Arne Weiss Date: Mon, 1 Jun 2026 08:52:53 +0200 Subject: [PATCH] Add CSS hook, URL ref prefill, order prefill, and Widerrufsfrist display MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- controllers/front/withdraw.php | 30 +++++++++++++----- simple_withdrawalbutton.php | 52 +++++++++++++++++++++++++++++++ upgrade/index.php | 8 +++++ upgrade/upgrade-0.1.5.php | 10 ++++++ views/templates/front/confirm.tpl | 11 +++++++ views/templates/front/form.tpl | 13 ++++++++ views/templates/front/success.tpl | 11 +++++++ 7 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 upgrade/index.php create mode 100644 upgrade/upgrade-0.1.5.php diff --git a/controllers/front/withdraw.php b/controllers/front/withdraw.php index 7cec38f..645fe9c 100644 --- a/controllers/front/withdraw.php +++ b/controllers/front/withdraw.php @@ -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 = []; diff --git a/simple_withdrawalbutton.php b/simple_withdrawalbutton.php index d47047f..90e71c8 100644 --- a/simple_withdrawalbutton.php +++ b/simple_withdrawalbutton.php @@ -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); diff --git a/upgrade/index.php b/upgrade/index.php new file mode 100644 index 0000000..30c3516 --- /dev/null +++ b/upgrade/index.php @@ -0,0 +1,8 @@ +registerHook('actionFrontControllerSetMedia'); +} diff --git a/views/templates/front/confirm.tpl b/views/templates/front/confirm.tpl index 8ac17fc..eedc841 100644 --- a/views/templates/front/confirm.tpl +++ b/views/templates/front/confirm.tpl @@ -70,6 +70,17 @@
{$form_data.message|escape:'html':'UTF-8'|nl2br nofilter}
{/if} + {if isset($withdrawal_deadline) && $withdrawal_deadline != ''} +
+
{l s='Frist' mod='simple_withdrawalbutton'}
+
+ {l s='bis' mod='simple_withdrawalbutton'} {$withdrawal_deadline|escape:'html':'UTF-8'} + + ({l s='14 Tage ab Bestelldatum' mod='simple_withdrawalbutton'}) + +
+
+ {/if}
diff --git a/views/templates/front/form.tpl b/views/templates/front/form.tpl index 864f705..8046957 100644 --- a/views/templates/front/form.tpl +++ b/views/templates/front/form.tpl @@ -28,6 +28,19 @@ {/if} + {if isset($withdrawal_deadline) && $withdrawal_deadline != ''} +
+ 📅 + + {l s='Widerrufsfrist (14 Tage ab Bestelldatum): bis' mod='simple_withdrawalbutton'} + {$withdrawal_deadline|escape:'html':'UTF-8'} + +
+ {/if} + {if $errors_list|@count > 0} {/if} + {if isset($withdrawal_deadline) && $withdrawal_deadline != ''} +
+
{l s='Frist' mod='simple_withdrawalbutton'}
+
+ {l s='bis' mod='simple_withdrawalbutton'} {$withdrawal_deadline|escape:'html':'UTF-8'} + + ({l s='14 Tage ab Bestelldatum' mod='simple_withdrawalbutton'}) + +
+
+ {/if}