Commit 1ae54516 by halweg

fix : 未订阅用户设置10min 推送一次订阅优惠

parent d5c864d2
......@@ -6,6 +6,7 @@
*/
namespace Joshine\CouponPusher\Controller\Ajax;
use Joshine\CouponPusher\Helper\CustomerChecker;
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
......@@ -50,6 +51,8 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
private $resultJsonFactory;
private $customerChecker;
/**
* Initialize dependencies.
*
......@@ -71,12 +74,14 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
CustomerAccountManagement $customerAccountManagement,
SubscriptionManagerInterface $subscriptionManager,
EmailValidator $emailValidator = null,
CustomerChecker $customerChecker,
JsonFactory $jsonFactory
) {
$this->customerAccountManagement = $customerAccountManagement;
$this->subscriptionManager = $subscriptionManager;
$this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class);
$this->resultJsonFactory = $jsonFactory;
$this->customerChecker = $customerChecker;
parent::__construct(
$context,
$subscriberFactory,
......@@ -184,6 +189,7 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
'data' => $message,
'coupon_code' => $this->getCouponCode()
];
$this->setSubscribeCookie();
} catch (LocalizedException $e) {
$message = ['error' => $e->getMessage()];
} catch (\Exception $e) {
......@@ -240,4 +246,9 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
ScopeInterface::SCOPE_STORE
);
}
private function setSubscribeCookie()
{
$this->customerChecker->setSubscribeCookie();
}
}
......@@ -2,6 +2,17 @@
namespace Joshine\CouponPusher\Helper;
class Constant {
const FIRST_VISITOR = 'first_visitor';
const OTHER = 'other';
const FIRST_VISITOR = 'first_visitor';
const HAS_SUBSCRIBE = 'has_subscribe';
const STOP_COUPON_PUSH = 'coupon_push_stop';
const NOT_SUBSCRIBE = 'not_subscribe';
const HAS_SUBSCRIBE_COOKIE_DURATION = 86400 * 365;
const HAS_VISITED_COOKIE_KEY = 'has_visited';
const HAS_VISITED_COOKIE_DURATION = 86400 * 365;
const OTHER = 'other';
}
<?php
namespace Joshine\CouponPusher\Helper;
class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInterface
{
const HAS_VISITED_COOKIE_KEY = 'has_visited';
const HAS_VISITED_COOKIE_DURATION = 86400 * 30;
protected $_cookieManager;
......@@ -29,28 +28,54 @@ class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInt
public function checkFirstVisit(): bool
{
$previousTimestamp = $this->_cookieManager->getCookie(self::HAS_VISITED_COOKIE_KEY);
if (!$previousTimestamp) {
$metadata = $this->_cookieMetadataFactory
->createPublicCookieMetadata()
->setPath('/')
->setDuration(self::HAS_VISITED_COOKIE_DURATION);
$this->_cookieManager->setPublicCookie(
self::HAS_VISITED_COOKIE_KEY,
'true',
$metadata
);
return true;
}
return false;
return is_null($this->_cookieManager->getCookie(Constant::HAS_VISITED_COOKIE_KEY));
}
public function setFirstVisitCookie()
{
$this->setCookie(Constant::HAS_VISITED_COOKIE_KEY, 'true', Constant::HAS_VISITED_COOKIE_DURATION);
}
public function checkSubscribed(): bool
{
return !is_null($this->_cookieManager->getCookie(Constant::HAS_SUBSCRIBE));
}
public function setSubscribeCookie()
{
$this->setCookie(Constant::HAS_SUBSCRIBE, 'true', Constant::HAS_SUBSCRIBE_COOKIE_DURATION);
}
public function setCookie($key, $data, $time)
{
$metadata = $this->_cookieMetadataFactory
->createPublicCookieMetadata()
->setPath('/')
->setDuration($time);
$this->_cookieManager->setPublicCookie(
$key,
$data,
$metadata
);
}
//访客细分
public function getCustomerSegments(): string
{
if ($this->checkFirstVisit()) {
$this->setFirstVisitCookie();
return Constant::FIRST_VISITOR;
}
if (!$this->checkSubscribed()) {
return Constant::NOT_SUBSCRIBE;
}
if ($this->checkSubscribed()) {
return Constant::HAS_SUBSCRIBE;
}
return Constant::OTHER;
}
}
......@@ -5,8 +5,8 @@ namespace Joshine\CouponPusher\Model;
use Joshine\CouponPusher\Helper\Constant;
use Joshine\CouponPusher\Helper\CustomerChecker;
use Joshine\CouponPusher\Model\Strategy\ContractPushStrategy;
use Joshine\CouponPusher\Model\Strategy\FirstVisitorPushStrategy;
use Joshine\CouponPusher\Model\Strategy\NopePushStrategy;
use Joshine\CouponPusher\Model\Strategy\SubscribePushStrategy;
use Joshine\CouponPusher\Model\Strategy\StopPushStrategy;
use Magento\Framework\ObjectManagerInterface;
class PushStrategyManager
......@@ -26,10 +26,18 @@ class PushStrategyManager
public function tagToStrategy($tag): string
{
$map = [
Constant::FIRST_VISITOR => FirstVisitorPushStrategy::class,
//第一次访问推送订阅券
Constant::FIRST_VISITOR => SubscribePushStrategy::class,
//未订阅,持续推送订阅券
Constant::NOT_SUBSCRIBE => SubscribePushStrategy::class,
//已订阅停止推送
Constant::HAS_SUBSCRIBE => StopPushStrategy::class,
];
if (!isset($map[$tag])) {
return NopePushStrategy::class;
return StopPushStrategy::class;
}
return $map[$tag];
}
......
......@@ -4,7 +4,7 @@ namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponNopeAlert;
class NopePushStrategy implements ContractPushStrategy
class StopPushStrategy implements ContractPushStrategy
{
/**
......
......@@ -3,23 +3,37 @@
namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponSubscribeAlert;
use Joshine\CouponPusher\Helper\Constant;
use Joshine\CouponPusher\Helper\CustomerChecker;
class FirstVisitorPushStrategy implements ContractPushStrategy
class SubscribePushStrategy implements ContractPushStrategy
{
/**
* @var \Magento\Framework\View\Element\BlockFactory
*/
private $blockFactory;
private $customerChecker;
public function __construct(
\Magento\Framework\View\Element\BlockFactory $blockFactory
\Magento\Framework\View\Element\BlockFactory $blockFactory,
CustomerChecker $customerChecker
)
{
$this->blockFactory = $blockFactory;
$this->customerChecker = $customerChecker;
}
public function push()
{
//停推10分钟
$this->stopPushTerm();
return $this->blockFactory->createBlock(CouponSubscribeAlert::class)->toHtml();
}
public function stopPushTerm()
{
$this->customerChecker->setCookie(Constant::STOP_COUPON_PUSH, 'true', 60 * 10);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment