如何在 magento 2 中设置时事通讯确认的到期时间 link

How to set expiration of newsletter confirmation link in magento 2

我的 magento 网站上有时事通讯。我已经从管理员配置订阅前启用确认。用户正在通过邮件收到确认 link。

但我想设置那个 link 的到期时间。 magento 是否提供默认配置?

如何设置 link 的到期时间?

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

1) 在newsletter_subscribertable中添加created_at字段。

2) 覆盖以下文件

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

Company/name/Model/Subscriber.php

覆盖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;
    }

希望对大家有所帮助。

谢谢。