Dodanie możliwości wypisania się z newslettera

This commit is contained in:
Roman Pyrih
2025-11-14 11:34:40 +01:00
parent 99ee04f426
commit 4a1e6a4424
4 changed files with 187 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

@@ -293,6 +293,11 @@
Wykorzystaj go na <b><a href="https://drmaterac.pl">drmaterac.pl</a></b> i ciesz się komfortowym snem!<br><br> Wykorzystaj go na <b><a href="https://drmaterac.pl">drmaterac.pl</a></b> i ciesz się komfortowym snem!<br><br>
Kod nie łączy się z innymi promocjami. Kod nie łączy się z innymi promocjami.
</div> </div>
<div style="font-family:Open sans, arial, sans-serif;font-size:12px;font-weight:400;line-height:18px;text-align:left;color:#363A41;" align="left">
<br><br>
Jeśli nie chcesz już otrzymywać naszego newslettera,
<a href="{unsubscribe_url}">wypisz się tutaj</a>.
</div>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@@ -819,16 +819,29 @@ class Ps_Emailsubscription extends Module implements WidgetInterface
* *
* @return bool * @return bool
*/ */
protected function sendConfirmationEmail($email, $code) function sendConfirmationEmail($email, $code)
{ {
$language = new Language($this->context->language->id); $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( return Mail::send(
$this->context->language->id, $this->context->language->id,
'newsletter_conf', 'newsletter_conf',
'Potwierdzenie zapisu do newslettera', 'Potwierdzenie zapisu do newslettera',
[ [
'{discount}' => $code, '{discount}' => $code,
'{unsubscribe_url}' => $unsubscribeUrl,
], ],
pSQL($email), pSQL($email),
null, null,
@@ -842,6 +855,7 @@ class Ps_Emailsubscription extends Module implements WidgetInterface
); );
} }
/** /**
* Send a verification email. * 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')); 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}