【问题标题】:How to set expiration of newsletter confirmation link in magento 2如何在magento 2中设置时事通讯确认链接的到期
【发布时间】:2018-05-03 05:46:29
【问题描述】:

我的 magento 网站上有时事通讯。我在从管理员配置订阅之前启用了确认。用户在邮件中收到确认链接。

但我想设置该链接的到期时间。 magento 是否提供默认配置?

如何设置该链接的过期时间?

【问题讨论】:

  • 我也有同样的问题...
  • 你有什么解决办法吗?
  • 还没有。我正在努力。
  • @ChanakaHettiarachchi 我得到了解决方案。也许它会帮助你....如果它对你有帮助,别忘了给答案投票:)

标签: magento2


【解决方案1】:

我找到了解决方案。我只做了两件事。

1) 在newsletter_subscriber 表中添加created_at 字段。

2) 覆盖以下文件

vendor/magento/module-newsletter/Model/Subscriber.php

公司/名称/型号/Subscriber.php

Overited Subscriber.php 文件代码

public function confirm($code) // existing function
    {
        $id = $this->getId();
        if ($this->validateConfirmLinkToken($id, $code)) {
            if ($this->getCode() == $code) {
                $this->setStatus(self::STATUS_SUBSCRIBED)
                    ->setStatusChanged(true)
                    ->save();
                $this->sendConfirmationSuccessEmail();
                return true;
            }

            return false;
        }
    }

    private function validateConfirmLinkToken($customerId, $code) //check validation for token
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $messageManager = $objectManager->get('Magento\Framework\Message\ManagerInterface');
        if (empty($customerId) || $customerId < 0) {
            $this->_messageManager->addError('Sorry you have not rigts to access this page');
            return false;
        }
        if (!is_string($code) || empty($code)) {
            $params = ['fieldName' => 'code'];
            //$messageManager->addError('Sorry Your subscription confirmation code is not valid.');
            return false;
        }
        $dcode = $this->getCode();
        $dcreated_at = $this->getCreatedAt();
        if (trim($dcode) != trim($code)) {
            //$messageManager->addError('Sorry Your subscription confirmation code is mismatch.');
            return false;
        } elseif ($this->isConfirmationLinkTokenExpired($dcode, $dcreated_at)) {
            //$messageManager->addError('Sorry Your subscription confirmation code is expired.');
            return false;

        }

        return true;
    }

    public function isConfirmationLinkTokenExpired($dcode, $dcreated_at) // check expiration token
    {
        if (empty($dcode) || empty($dcreated_at)) {
            return true;
        }

        $expirationPeriod = '720';

        $currentTimestamp = (new \DateTime())->getTimestamp();
        $tokenTimestamp = (new \DateTime($dcreated_at))->getTimestamp();
        if ($tokenTimestamp > $currentTimestamp) {
            return true;
        }

        $hourDifference = floor(($currentTimestamp - $tokenTimestamp) / (60 * 60));
        if ($hourDifference >= $expirationPeriod) {
            return true;
        }

        return false;
    }

希望对很多人有所帮助。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2013-07-26
    • 2022-11-06
    • 2011-04-04
    • 2011-09-11
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多