FIX: newsletter - adding unsubscribe link

This commit is contained in:
Roman Pyrih
2026-01-30 15:47:20 +01:00
parent bfe4083161
commit 7a26dd69a5
4 changed files with 188 additions and 2 deletions

View File

@@ -0,0 +1,34 @@
<?php
class Ps_EmailsubscriptionUnsubscribeModuleFrontController extends ModuleFrontController
{
/** @var string */
protected $message = '';
public function init()
{
parent::init();
$token = Tools::getValue('token');
if (!$token) {
$this->message = $this->module->trans(
'Invalid unsubscribe link.',
[],
'Modules.Emailsubscription.Shop'
);
} else {
$this->message = $this->module->unsubscribeByToken($token);
}
}
public function initContent()
{
parent::initContent();
$this->context->smarty->assign('message', $this->message);
$this->setTemplate(
'module:ps_emailsubscription/views/templates/front/unsubscribe_execution.tpl'
);
}
}

View File

@@ -292,6 +292,12 @@
🎁 Oto Twój kod rabatowy na pierwsze zakupy: <b>{discount}</b><br><br>
Wykorzystaj go na <b>lulandia.pl</b> i ciesz się komfortowym snem!
</div>
<br>
<div style="font-family:Open sans, arial, sans-serif;font-size:12px;font-weight:400;line-height:18px;text-align:left;color:#aeaeae;" align="left">
<br><br>
Nie chcemy zakłócać Twojego spokoju ????<br>
Jeśli wolisz, by nasze wiadomości Cię nie budziły,<a href="{unsubscribe_url}">Wypisz mnie</a>.
</div>
</td>
</tr>
</tbody>

View File

@@ -819,16 +819,29 @@ class Ps_Emailsubscription extends Module implements WidgetInterface
*
* @return bool
*/
protected function sendConfirmationEmail($email, $code)
function sendConfirmationEmail($email, $code)
{
$language = new Language($this->context->language->id);
$unsubscribeToken = $this->getUnsubscribeTokenByEmail($email);
$unsubscribeUrl = '';
if ($unsubscribeToken) {
$unsubscribeUrl = Context::getContext()->link->getModuleLink(
'ps_emailsubscription',
'unsubscribe',
['token' => $unsubscribeToken],
true
);
}
return Mail::send(
$this->context->language->id,
'newsletter_conf',
'Potwierdzenie zapisu do newslettera',
[
'{discount}' => $code,
'{discount}' => $code,
'{unsubscribe_url}' => $unsubscribeUrl,
],
pSQL($email),
null,
@@ -842,6 +855,7 @@ class Ps_Emailsubscription extends Module implements WidgetInterface
);
}
/**
* Send a verification email.
*
@@ -1470,4 +1484,105 @@ class Ps_Emailsubscription extends Module implements WidgetInterface
return json_encode($this->trans('Newsletter subscription: no email to export, this customer has not registered.', [], 'Modules.Emailsubscription.Admin'));
}
}
protected function getGuestEmailByUnsubscribeToken($token)
{
$sql = 'SELECT `email`
FROM `' . _DB_PREFIX_ . 'emailsubscription`
WHERE MD5(CONCAT(`email`, `newsletter_date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\'))
= \'' . pSQL($token) . '\'
AND `active` = 1';
return Db::getInstance()->getValue($sql);
}
/**
* Customer email for unsubscribe token.
*/
protected function getUserEmailByUnsubscribeToken($token)
{
$sql = 'SELECT `email`
FROM `' . _DB_PREFIX_ . 'customer`
WHERE MD5(CONCAT(`email`, `date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\'))
= \'' . pSQL($token) . '\'
AND `newsletter` = 1';
return Db::getInstance()->getValue($sql);
}
/**
* Unsubscribe by token from email.
*
* @param string $token
*
* @return string message
*/
public function unsubscribeByToken($token)
{
$email = null;
$isGuest = false;
if ($email = $this->getGuestEmailByUnsubscribeToken($token)) {
$isGuest = true;
} elseif ($email = $this->getUserEmailByUnsubscribeToken($token)) {
$isGuest = false;
} else {
return $this->trans(
'Nieprawidłowy lub wygasły link do rezygnacji z subskrypcji.',
[],
'Modules.Emailsubscription.Shop'
);
}
if ($isGuest) {
$sql = 'DELETE FROM `' . _DB_PREFIX_ . 'emailsubscription`
WHERE `email` = \'' . pSQL($email) . '\'
AND `id_shop` = ' . (int) $this->context->shop->id;
} else {
$sql = 'UPDATE `' . _DB_PREFIX_ . 'customer`
SET `newsletter` = 0
WHERE `email` = \'' . pSQL($email) . '\'
AND `id_shop` = ' . (int) $this->context->shop->id;
}
if (!Db::getInstance()->execute($sql)) {
return $this->trans(
'Wystąpił błąd podczas próby rezygnacji z subskrypcji.',
[],
'Modules.Emailsubscription.Shop'
);
}
return $this->trans(
'Wypisałeś się z newslettera.',
[],
'Modules.Emailsubscription.Shop'
);
}
/**
* Token used in unsubscribe link (for active subscribers).
*/
protected function getUnsubscribeTokenByEmail($email)
{
// guest subscription
$sql = 'SELECT MD5(CONCAT(`email`, `newsletter_date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) AS token
FROM `' . _DB_PREFIX_ . 'emailsubscription`
WHERE `active` = 1
AND `email` = \'' . pSQL($email) . '\'
AND `id_shop` = ' . (int) $this->context->shop->id;
$token = Db::getInstance()->getValue($sql);
if ($token) {
return $token;
}
// customer subscription
$sql = 'SELECT MD5(CONCAT(`email`, `date_add`, \'' . pSQL(Configuration::get('NW_SALT')) . '\')) AS token
FROM `' . _DB_PREFIX_ . 'customer`
WHERE `newsletter` = 1
AND `email` = \'' . pSQL($email) . '\'
AND `id_shop` = ' . (int) $this->context->shop->id;
return Db::getInstance()->getValue($sql);
}
}

View File

@@ -0,0 +1,31 @@
{extends file='page.tpl'}
{block name="page_title"}
{l s='Newsletter' d='Modules.Emailsubscription.Shop'}
{/block}
{block name="page_content"}
<section id="unsubscribe-page" class="page-content">
<header class="page-header">
<h1 class="h1">
{l s='Wypisanie z newslettera' d='Modules.Emailsubscription.Shop'}
</h1>
</header>
<p class="alert alert-success">
{$message|escape:'htmlall':'UTF-8'}
</p>
<p>
{l s='Nie będziesz już otrzymywać od nas wiadomości newsletterowych na ten adres e-mail.' d='Modules.Emailsubscription.Shop'}
</p>
<p>
{l s='Jeśli wypisałeś(-aś) się przez pomyłkę, możesz w każdej chwili ponownie zapisać się do newslettera.' d='Modules.Emailsubscription.Shop'}
</p>
<a href="{$urls.base_url}" class="btn btn-primary">
{l s='Przejdź do strony głównej' d='Modules.Emailsubscription.Shop'}
</a>
</section>
{/block}