Commit 1ae54516 by halweg

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

parent d5c864d2
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
namespace Joshine\CouponPusher\Controller\Ajax; namespace Joshine\CouponPusher\Controller\Ajax;
use Joshine\CouponPusher\Helper\CustomerChecker;
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement; use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session; use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl; use Magento\Customer\Model\Url as CustomerUrl;
...@@ -50,6 +51,8 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface ...@@ -50,6 +51,8 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
private $resultJsonFactory; private $resultJsonFactory;
private $customerChecker;
/** /**
* Initialize dependencies. * Initialize dependencies.
* *
...@@ -71,12 +74,14 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface ...@@ -71,12 +74,14 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
CustomerAccountManagement $customerAccountManagement, CustomerAccountManagement $customerAccountManagement,
SubscriptionManagerInterface $subscriptionManager, SubscriptionManagerInterface $subscriptionManager,
EmailValidator $emailValidator = null, EmailValidator $emailValidator = null,
CustomerChecker $customerChecker,
JsonFactory $jsonFactory JsonFactory $jsonFactory
) { ) {
$this->customerAccountManagement = $customerAccountManagement; $this->customerAccountManagement = $customerAccountManagement;
$this->subscriptionManager = $subscriptionManager; $this->subscriptionManager = $subscriptionManager;
$this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class); $this->emailValidator = $emailValidator ?: ObjectManager::getInstance()->get(EmailValidator::class);
$this->resultJsonFactory = $jsonFactory; $this->resultJsonFactory = $jsonFactory;
$this->customerChecker = $customerChecker;
parent::__construct( parent::__construct(
$context, $context,
$subscriberFactory, $subscriberFactory,
...@@ -184,6 +189,7 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface ...@@ -184,6 +189,7 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
'data' => $message, 'data' => $message,
'coupon_code' => $this->getCouponCode() 'coupon_code' => $this->getCouponCode()
]; ];
$this->setSubscribeCookie();
} catch (LocalizedException $e) { } catch (LocalizedException $e) {
$message = ['error' => $e->getMessage()]; $message = ['error' => $e->getMessage()];
} catch (\Exception $e) { } catch (\Exception $e) {
...@@ -240,4 +246,9 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface ...@@ -240,4 +246,9 @@ class Subscribe extends SubscriberController implements HttpPostActionInterface
ScopeInterface::SCOPE_STORE ScopeInterface::SCOPE_STORE
); );
} }
private function setSubscribeCookie()
{
$this->customerChecker->setSubscribeCookie();
}
} }
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
namespace Joshine\CouponPusher\Helper; namespace Joshine\CouponPusher\Helper;
class Constant { class Constant {
const FIRST_VISITOR = 'first_visitor'; const FIRST_VISITOR = 'first_visitor';
const OTHER = 'other'; 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 <?php
namespace Joshine\CouponPusher\Helper; namespace Joshine\CouponPusher\Helper;
class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInterface class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInterface
{ {
const HAS_VISITED_COOKIE_KEY = 'has_visited';
const HAS_VISITED_COOKIE_DURATION = 86400 * 30;
protected $_cookieManager; protected $_cookieManager;
...@@ -29,28 +28,54 @@ class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInt ...@@ -29,28 +28,54 @@ class CustomerChecker implements \Magento\Framework\Data\CollectionDataSourceInt
public function checkFirstVisit(): bool public function checkFirstVisit(): bool
{ {
$previousTimestamp = $this->_cookieManager->getCookie(self::HAS_VISITED_COOKIE_KEY); return is_null($this->_cookieManager->getCookie(Constant::HAS_VISITED_COOKIE_KEY));
if (!$previousTimestamp) { }
$metadata = $this->_cookieMetadataFactory
->createPublicCookieMetadata() public function setFirstVisitCookie()
->setPath('/') {
->setDuration(self::HAS_VISITED_COOKIE_DURATION); $this->setCookie(Constant::HAS_VISITED_COOKIE_KEY, 'true', Constant::HAS_VISITED_COOKIE_DURATION);
$this->_cookieManager->setPublicCookie( }
self::HAS_VISITED_COOKIE_KEY,
'true',
$metadata public function checkSubscribed(): bool
); {
return true; return !is_null($this->_cookieManager->getCookie(Constant::HAS_SUBSCRIBE));
} }
return false;
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 public function getCustomerSegments(): string
{ {
if ($this->checkFirstVisit()) { if ($this->checkFirstVisit()) {
$this->setFirstVisitCookie();
return Constant::FIRST_VISITOR; return Constant::FIRST_VISITOR;
} }
if (!$this->checkSubscribed()) {
return Constant::NOT_SUBSCRIBE;
}
if ($this->checkSubscribed()) {
return Constant::HAS_SUBSCRIBE;
}
return Constant::OTHER; return Constant::OTHER;
} }
} }
...@@ -5,8 +5,8 @@ namespace Joshine\CouponPusher\Model; ...@@ -5,8 +5,8 @@ namespace Joshine\CouponPusher\Model;
use Joshine\CouponPusher\Helper\Constant; use Joshine\CouponPusher\Helper\Constant;
use Joshine\CouponPusher\Helper\CustomerChecker; use Joshine\CouponPusher\Helper\CustomerChecker;
use Joshine\CouponPusher\Model\Strategy\ContractPushStrategy; use Joshine\CouponPusher\Model\Strategy\ContractPushStrategy;
use Joshine\CouponPusher\Model\Strategy\FirstVisitorPushStrategy; use Joshine\CouponPusher\Model\Strategy\SubscribePushStrategy;
use Joshine\CouponPusher\Model\Strategy\NopePushStrategy; use Joshine\CouponPusher\Model\Strategy\StopPushStrategy;
use Magento\Framework\ObjectManagerInterface; use Magento\Framework\ObjectManagerInterface;
class PushStrategyManager class PushStrategyManager
...@@ -26,10 +26,18 @@ class PushStrategyManager ...@@ -26,10 +26,18 @@ class PushStrategyManager
public function tagToStrategy($tag): string public function tagToStrategy($tag): string
{ {
$map = [ $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])) { if (!isset($map[$tag])) {
return NopePushStrategy::class; return StopPushStrategy::class;
} }
return $map[$tag]; return $map[$tag];
} }
......
...@@ -4,7 +4,7 @@ namespace Joshine\CouponPusher\Model\Strategy; ...@@ -4,7 +4,7 @@ namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponNopeAlert; use Joshine\CouponPusher\Block\CouponNopeAlert;
class NopePushStrategy implements ContractPushStrategy class StopPushStrategy implements ContractPushStrategy
{ {
/** /**
......
...@@ -3,23 +3,37 @@ ...@@ -3,23 +3,37 @@
namespace Joshine\CouponPusher\Model\Strategy; namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponSubscribeAlert; 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 * @var \Magento\Framework\View\Element\BlockFactory
*/ */
private $blockFactory; private $blockFactory;
private $customerChecker;
public function __construct( public function __construct(
\Magento\Framework\View\Element\BlockFactory $blockFactory \Magento\Framework\View\Element\BlockFactory $blockFactory,
CustomerChecker $customerChecker
) )
{ {
$this->blockFactory = $blockFactory; $this->blockFactory = $blockFactory;
$this->customerChecker = $customerChecker;
} }
public function push() public function push()
{ {
//停推10分钟
$this->stopPushTerm();
return $this->blockFactory->createBlock(CouponSubscribeAlert::class)->toHtml(); 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