Commit 466b3105 by halweg

feat : coupon puhser

parent a01c82ae
<?php
namespace Joshine\CouponPusher\Block;
class CouponNopeAlert extends \Magento\Framework\View\Element\Template
{
protected $_template = 'Joshine_CouponPusher::coupon-nope-alert.phtml';
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Block;
use Magento\Framework\View\Element\Template;
class CouponSubscribeAlert extends \Magento\Framework\View\Element\Template
{
protected $_template = 'Joshine_CouponPusher::coupon-subscribe-alert.phtml';
protected $coupon;
protected $saleRule;
public function __construct(
Template\Context $context,
array $data = [],
\Magento\SalesRule\Model\Coupon $coupon,
\Magento\SalesRule\Model\Rule $saleRule
)
{
parent::__construct($context, $data);
$this->coupon = $coupon;
$this->saleRule = $saleRule;
}
public function getCoupon()
{
$ruleId = $this->coupon->loadByCode('N20F')->getRuleId();
return $this->saleRule->load($ruleId);
}
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Block;
class JsInit extends \Magento\Framework\View\Element\Template {
protected $_template = 'Joshine_CouponPusher::js-init.phtml';
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Controller\Ajax;
use Joshine\CouponPusher\Helper\CustomerChecker;
use Joshine\CouponPusher\Model\PushStrategyManager;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
class CouponPusher extends \Magento\Framework\App\Action\Action {
protected $subscriptionManager;
protected $customerChecker;
protected $pushStrategyManager;
protected $formKeyValidator;
protected $remoteAddress;
protected $logger;
protected $jsonEncoder;
protected $resultJsonFactory;
public function __construct(
Context $context,
CustomerChecker $customerChecker,
PushStrategyManager $pushStrategyManager,
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
JsonFactory $resultJsonFactory
)
{
parent::__construct($context);
$this->customerChecker = $customerChecker;
$this->pushStrategyManager = $pushStrategyManager;
$this->formKeyValidator = $formKeyValidator;
$this->remoteAddress = $remoteAddress;
$this->logger = $logger;
$this->jsonEncoder = $jsonEncoder;
$this->resultJsonFactory = $resultJsonFactory;
}
public function execute()
{
$message = [
'error' => __('Sorry. There is a problem with your Request.')
];
if ($this->getRequest()->isAjax()) {
try {
if (!$this->formKeyValidator->validate($this->getRequest())) {
throw new LocalizedException(
__('Form key is not valid. Please try to reload the page.')
);
}
$message = [
'success' => __('Success.'),
'data' => $this->pushStrategyManager->getPushTemplate(),
];
} catch (LocalizedException $e) {
$message = ['error' => $e->getMessage()];
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
$resultPage = $this->resultJsonFactory->create();
$resultPage->setHttpResponseCode(200);
$resultPage->setData($message);
return $resultPage;
}
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Helper;
class Constant {
const FIRST_VISITOR = 'first_visitor';
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;
protected $_dateTime;
protected $_cookieMetadataFactory;
private $_request;
public function __construct(
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
\Magento\Framework\App\RequestInterface $request
)
{
$this->_cookieManager = $cookieManager;
$this->_cookieMetadataFactory = $cookieMetadataFactory;
$this->_dateTime = $dateTime;
$this->_request = $request;
}
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;
}
//访客细分
public function getCustomerSegments(): string
{
if ($this->checkFirstVisit()) {
return Constant::FIRST_VISITOR;
}
return Constant::OTHER;
}
}
<?php
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 Magento\Framework\ObjectManagerInterface;
class PushStrategyManager
{
protected $_customerChecker;
protected $_objectManger;
public function __construct(
CustomerChecker $customerChecker,
ObjectManagerInterface $objectManager
)
{
$this->_customerChecker = $customerChecker;
$this->_objectManger = $objectManager;
}
public function tagToStrategy($tag): string
{
$map = [
Constant::FIRST_VISITOR => FirstVisitorPushStrategy::class,
];
if (!isset($map[$tag])) {
return NopePushStrategy::class;
}
return $map[$tag];
}
public function getPushTemplate() {
$tag = $this->_customerChecker->getCustomerSegments();
$strategyName = $this->tagToStrategy($tag);
/**
* @var $strategy ContractPushStrategy
*/
$strategy = $this->_objectManger->create($strategyName);
return $strategy->push();
}
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Model\Strategy;
interface ContractPushStrategy
{
public function push();
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponSubscribeAlert;
class FirstVisitorPushStrategy implements ContractPushStrategy
{
/**
* @var \Magento\Framework\View\Element\BlockFactory
*/
private $blockFactory;
public function __construct(
\Magento\Framework\View\Element\BlockFactory $blockFactory
)
{
$this->blockFactory = $blockFactory;
}
public function push()
{
return $this->blockFactory->createBlock(CouponSubscribeAlert::class)->toHtml();
}
}
\ No newline at end of file
<?php
namespace Joshine\CouponPusher\Model\Strategy;
use Joshine\CouponPusher\Block\CouponNopeAlert;
class NopePushStrategy implements ContractPushStrategy
{
/**
* @var \Magento\Framework\View\Element\BlockFactory
*/
private $blockFactory;
public function __construct(
\Magento\Framework\View\Element\BlockFactory $blockFactory
)
{
$this->blockFactory = $blockFactory;
}
public function push()
{
return $this->blockFactory->createBlock(CouponNopeAlert::class)->toHtml();
}
}
\ No newline at end of file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="joshine_coupon_pusher" frontName="joshine_coupon_pusher">
<module name="Joshine_CouponPusher" />
</route>
</router>
</config>
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Joshine_CouponPusher" setup_version="0.0.1">>
</module>
</config>
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Joshine_CouponPusher',
__DIR__
);
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="Joshine_CouponPusher::css/_coupon_alert.css"/>
</head>
<referenceContainer name="footer-container">
<block class="Joshine\CouponPusher\Block\JsInit" name="coupon.pusher.js"/>
</referenceContainer>
</page>
\ No newline at end of file
<script type="text/javascript">
require(['jquery', 'mage/cookies'], function($) {
if ($.mage.cookies.get('coupon_push_stop') == null) {
// $.mage.cookies.set('coupon_push_stop', 'true');
}
})
</script>
<?php
/**
* @var $block \Joshine\CouponPusher\Block\CouponSubscribeAlert
*/
?>
<div class="joshine-model-warp" id="coupon-subscribe-alert" style="display: none;">
<div class="joshine-mask"></div>
<div class="j-modal j-modal-vertical">
<div class="j-modal-dialog j-modal-fluid">
<div class="j-modal-content">
<div class="j-modal-header">
<button title="Close (Esc)" type="button" class="sparsh-mfp-close" id="coupon-alert-close"></button>
</div>
<div class="j-modal-body">
<div class="coupon-alt joshine-text-center">
<img src="<?= $block->getViewFileUrl('Joshine_CouponPusher::images/coupon_background.png'); ?>" alt="" class="">
<div class="coupon-text">
<p class="coupon-discount-amount">20% OFF</p>
<p class="coupon-discount-rule">testtest</p>
</div>
<div class="coupon-alt-input-title">
<?= __('Sign up for text messages to get your exclusive offer.') ?>
</div>
<div class="coupon-alt-input">
<div class="coupon-alt-input-left">
<input placeholder="Enter your email address" type="email" value="">
</div>
<div class="coupon-alert-submit-button"><?= __('Submit') ?></div>
</div>
<div class="split-line"><span class="split-text"><?= __('OR') ?></span></div>
<div class="coupon-alt-bottom-title"><span><?= __('Submit email address to get your exclusive offer.') ?></span>
</div>
<div class="coupon-alt-bottom-text">
<?= __('*You agree to receive autodialed marketing messages on this
number. Consent not required to purchase goods. Message and data rates may apply. Message
frequency may vary. Text STOP to opt out and HELP for help.') ?>
<a href="#" class="" target="_blank"><?= __('Privacy policy') ?> </a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
require(['jquery'], function($) {
setTimeout(function () {
$("#coupon-subscribe-alert").show();
}, 2000);
$(document).on('click', '#coupon-alert-close', function () {
$('#coupon-subscribe-alert').hide();
});
})
</script>
<script type="text/javascript">
require(['jquery','mage/cookies'], function($) {
function getCouponAlert() {
var formKey = $.mage.cookies.get('form_key');
var data = 'form_key=' + formKey;
$.ajax({
url: '/joshine_coupon_pusher/ajax/couponpusher',
data: data,
type: 'GET',
dataType: 'json',
success: function (response) {
$('body').append(response.data);
}
});
}
function couponPusherStart()
{
var stopPush = $.mage.cookies.get('coupon_push_stop');
if (stopPush == 'true') {
return
}
setTimeout(function () {
getCouponAlert();
}, 1500)
}
couponPusherStart();
})
</script>
#coupon-subscribe-alert.joshine-model-warp .j-modal .j-modal-dialog {
width: 640px;
}
#coupon-subscribe-alert .j-modal .j-modal-content {
padding: 0;
}
.coupon-alt {
position: relative;
padding-bottom: 25px;
background: #fff;
border-radius: 4px;
display: flex;
flex-direction: column;
align-items: center;
}
.coupon-alt img {
height: 142px;
margin-top: 22px;
}
.coupon-alt .coupon-text {
position: absolute;
top: 58px;
left: 0;
width: 100%;
text-align: center;
}
.coupon-alt .coupon-text .coupon-discount-amount {
font-size: 44px;
color: #66331e;
line-height: 48px;
font-weight: 700;
}
.coupon-alt .coupon-text .coupon-discount-rule {
font-size: 12px;
color: #a59176;
line-height: 18px;
}
.coupon-alt .coupon-alt-input-title {
margin-top: 4px;
margin-bottom: 14px;
height: 22px;
font-size: 18px;
color: #222;
line-height: 22px;
font-weight: 500;
}
.coupon-alt .coupon-alt-input {
width: 446px;
height: 36px;
display: flex;
border-radius: 4px;
}
.coupon-alt .coupon-alt-input .coupon-alt-input-left {
border: 1px solid #dfdfdf;
border-right-width: 0;
flex: 1;
display: flex;
align-items: center;
border-radius: 4px 0 0 4px;
}
.coupon-alt-input-left .coupon-alt-input-left-prefix {
display: flex;
align-items: center;
justify-content: center;
line-height: 16px;
font-weight: 500;
padding: 0 11px;
font-size: 14px;
color: #000;
border-right: 1px solid #dfdfdf;
}
.coupon-alt-input-left input {
height: 16px;
font-size: 13px;
color: #000;
line-height: 16px;
font-weight: 500;
width: 240px;
border: 0;
}
.coupon-alert-submit-button {
position: relative;
left: -3px;
width: 116px;
height: 36px;
background: #fb7701;
border-radius: 0 4px 4px 0;
color: #fff;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
line-height: 16px;
font-weight: 500;
}
.coupon-alt-bottom-title {
height: 16px;
font-size: 14px;
color: #222;
font-weight: 500;
margin-top: 16px;
margin-bottom: 12px;
}
.coupon-alt-bottom-text {
opacity: .9;
font-size: 11px;
color: #afafaf;
line-height: 16px;
font-weight: 400;
text-align: center;
padding: 0 60px;
}
.coupon-alt-bottom-text >a {
color: #777;
}
\ 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