如何在magento 2中设置新闻通讯确认链接的到期日

时间:2018-05-03 05:46:29

标签: magento2

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

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

如何设置该链接的到期日期?

1 个答案:

答案 0 :(得分:1)

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

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

2)覆盖以下文件

  

供应商/的magento /模块的通讯/型号/ 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;
    }

希望它会对很多人有所帮助。

感谢。