This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Block\Adminhtml\System\Config\Form\Field;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
class Date extends Field
{
/**
* Set an explicit, locale-independent format before rendering the calendar field.
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element): string
{
$element->setDateFormat('yyyy-MM-dd');
return parent::_getElementHtml($element);
}
}
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Block;
use Esun\PromotionPopup\Model\Config;
use Magento\Cms\Block\BlockByIdentifier;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
class Popup extends Template
{
public function __construct(
Context $context,
private readonly Config $config,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
}
public function isAvailable(): bool
{
return $this->config->canDisplay();
}
public function getCmsContent(): string
{
$identifier = $this->config->getCmsBlockIdentifier();
if ($identifier === '') {
return '';
}
$content = $this->getLayout()->createBlock(
BlockByIdentifier::class,
'esun.promotion.popup.cms.' . sha1($identifier)
);
if (!$content) {
return '';
}
return $content->setData('identifier', $identifier)->toHtml();
}
public function getJsonConfig(): string
{
return $this->json->serialize([
'websiteId' => $this->config->getWebsiteId(),
'campaignKey' => sha1($this->config->getCmsBlockIdentifier()),
'campaignVersion' => $this->config->getCampaignVersion(),
'displayDelay' => $this->config->getDisplayDelay(),
'frequency' => $this->config->getFrequency(),
'audience' => $this->config->getAudience(),
]);
}
}
@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model;
use Esun\PromotionPopup\Model\Config\Source\Audience;
use Esun\PromotionPopup\Model\Config\Source\Frequency;
use Esun\PromotionPopup\Model\Config\Source\PromotionType;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
private const XML_PATH_PREFIX = 'esun_promotion_popup/general/';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly StoreManagerInterface $storeManager,
private readonly TimezoneInterface $timezone,
private readonly RequestInterface $request,
private readonly GetBlockByIdentifierInterface $blockByIdentifier
) {
}
public function canDisplay(): bool
{
if (!$this->isCampaignActive()) {
return false;
}
return !in_array($this->request->getFullActionName(), $this->getExcludedHandles(), true);
}
public function isCampaignActive(): bool
{
$identifier = $this->getCmsBlockIdentifier();
$store = $this->storeManager->getStore();
if (!$this->isEnabled()
|| $identifier === ''
|| !$this->timezone->isScopeDateInInterval(
$store,
$this->getValue('start_date') ?: null,
$this->getValue('end_date') ?: null
)
) {
return false;
}
try {
return $this->blockByIdentifier->execute($identifier, (int)$store->getId())->isActive();
} catch (NoSuchEntityException) {
return false;
}
}
public function isRegistrationNewsletterCouponPromotion(): bool
{
return $this->getValue('promotion_type') === PromotionType::REGISTRATION_NEWSLETTER_COUPON;
}
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_PREFIX . 'enabled',
ScopeInterface::SCOPE_STORE
);
}
public function getCmsBlockIdentifier(): string
{
return trim($this->getValue('cms_block_identifier'));
}
public function getWebsiteId(): int
{
return (int)$this->storeManager->getStore()->getWebsiteId();
}
public function getDisplayDelay(): int
{
return min(60000, max(0, (int)$this->getValue('display_delay')));
}
public function getFrequency(): string
{
$frequency = $this->getValue('frequency');
$allowed = [Frequency::ALWAYS, Frequency::SESSION, Frequency::DAILY, Frequency::CAMPAIGN];
return in_array($frequency, $allowed, true) ? $frequency : Frequency::DAILY;
}
public function getAudience(): string
{
return $this->getValue('audience') === Audience::ALL ? Audience::ALL : Audience::GUEST;
}
public function getCampaignVersion(): int
{
return max(1, (int)$this->getValue('campaign_version'));
}
/** @return string[] */
private function getExcludedHandles(): array
{
return array_values(array_unique(array_filter(array_map(
static fn(string $handle): string => trim($handle),
preg_split('/[\r\n,]+/', $this->getValue('excluded_handles')) ?: []
))));
}
private function getValue(string $field): string
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PREFIX . $field,
ScopeInterface::SCOPE_STORE
);
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class Audience implements OptionSourceInterface
{
public const GUEST = 'guest';
public const ALL = 'all';
public function toOptionArray(): array
{
return [
['value' => self::GUEST, 'label' => __('Guests Only')],
['value' => self::ALL, 'label' => __('All Visitors')],
];
}
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model\Config\Source;
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;
class CmsBlock implements OptionSourceInterface
{
public function __construct(private readonly CollectionFactory $collectionFactory)
{
}
public function toOptionArray(): array
{
$collection = $this->collectionFactory->create();
$collection->addFieldToSelect(['title', 'identifier']);
$collection->addFieldToFilter('is_active', 1);
$collection->setOrder('title', 'ASC');
$options = [['value' => '', 'label' => __('-- Please Select --')]];
$identifiers = [];
foreach ($collection as $block) {
$identifier = trim((string)$block->getIdentifier());
if ($identifier === '' || isset($identifiers[$identifier])) {
continue;
}
$identifiers[$identifier] = true;
$options[] = [
'value' => $identifier,
'label' => sprintf('%s [%s]', (string)$block->getTitle(), $identifier),
];
}
return $options;
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class Frequency implements OptionSourceInterface
{
public const ALWAYS = 'always';
public const SESSION = 'session';
public const DAILY = 'daily';
public const CAMPAIGN = 'campaign';
public function toOptionArray(): array
{
return [
['value' => self::DAILY, 'label' => __('Once per Day')],
['value' => self::SESSION, 'label' => __('Once per Session')],
['value' => self::CAMPAIGN, 'label' => __('Once per Campaign Version')],
['value' => self::ALWAYS, 'label' => __('Every Eligible Page View')],
];
}
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Esun\PromotionPopup\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class PromotionType implements OptionSourceInterface
{
public const CONTENT_ONLY = 'content_only';
public const REGISTRATION_NEWSLETTER_COUPON = 'registration_newsletter_coupon';
public function toOptionArray(): array
{
return [
[
'value' => self::REGISTRATION_NEWSLETTER_COUPON,
'label' => __('Registration + Newsletter Coupon'),
],
['value' => self::CONTENT_ONLY, 'label' => __('Content Only')],
];
}
}
+44
View File
@@ -0,0 +1,44 @@
# Esun Promotion Popup
Configure each Website at `Stores > Configuration > Esun > Promotion Popup`.
The selected CMS Block owns the promotion image, copy and CTA. The module owns the modal shell,
display schedule, audience, frequency, accessibility behavior and responsive styling.
## Initial CMS Block
Upload these images in the Media Gallery:
- `wysiwyg/promotion/welcome-24-desktop.jpg`
- `wysiwyg/promotion/welcome-24-mobile.jpg`
Then create an active CMS Block and add the following HTML in a Page Builder HTML Code element:
```html
<section class="promotion-popup-content promotion-popup-content--welcome"
aria-labelledby="welcome-promotion-title">
<div class="promotion-popup-content__media">
<picture>
<source media="(max-width: 767px)"
srcset="{{media url='wysiwyg/promotion/welcome-24-mobile.jpg'}}">
<img src="{{media url='wysiwyg/promotion/welcome-24-desktop.jpg'}}"
alt="eSUN new customer promotion"
loading="eager">
</picture>
</div>
<div class="promotion-popup-content__body">
<p class="promotion-popup-content__eyebrow">NEW CUSTOMER EXCLUSIVE</p>
<h2 class="promotion-popup-content__title" id="welcome-promotion-title">Get $24 Off</h2>
<p class="promotion-popup-content__condition">On Orders Over $299</p>
<p class="promotion-popup-content__description">
Create your eSUN account and subscribe to our newsletter to receive an exclusive $24 coupon.
</p>
<a class="promotion-popup-content__button" href="{{store url='customer/account/create/'}}">
Create Account
</a>
<p class="promotion-popup-content__terms">
New customers only. Coupon will be added to My Coupons after successful registration and subscription.
</p>
</div>
</section>
```
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Esun_PromotionPopup::configuration"
title="Promotion Popup Configuration"
sortOrder="95"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
@@ -0,0 +1,85 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="esun" translate="label" sortOrder="300">
<label>Esun</label>
</tab>
<section id="esun_promotion_popup" translate="label" type="text" sortOrder="370"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Promotion Popup</label>
<tab>esun</tab>
<resource>Esun_PromotionPopup::configuration</resource>
<group id="general" translate="label" type="text" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Website Promotion</label>
<field id="enabled" translate="label" type="select" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Enable Promotion Popup</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="cms_block_identifier" translate="label comment" type="select" sortOrder="20"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>CMS Block</label>
<comment>The selected block controls the promotion image, copy and CTA URL. Create localized blocks with the same identifier for different Store Views.</comment>
<source_model>Esun\PromotionPopup\Model\Config\Source\CmsBlock</source_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="promotion_type" translate="label comment" type="select" sortOrder="30"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Promotion Type</label>
<comment>Controls promotion-specific behavior outside the popup. Select Content Only for campaigns that do not grant a registration subscription coupon.</comment>
<source_model>Esun\PromotionPopup\Model\Config\Source\PromotionType</source_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="start_date" translate="label comment" type="date" sortOrder="40"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Start Date</label>
<comment>Optional. Uses the Website locale and timezone.</comment>
<frontend_model>Esun\PromotionPopup\Block\Adminhtml\System\Config\Form\Field\Date</frontend_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="end_date" translate="label comment" type="date" sortOrder="50"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>End Date</label>
<comment>Optional. The promotion remains active through the end of this date.</comment>
<frontend_model>Esun\PromotionPopup\Block\Adminhtml\System\Config\Form\Field\Date</frontend_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="audience" translate="label" type="select" sortOrder="60"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Audience</label>
<source_model>Esun\PromotionPopup\Model\Config\Source\Audience</source_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="display_delay" translate="label comment" type="text" sortOrder="70"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Display Delay (milliseconds)</label>
<comment>Recommended: 1500. Maximum: 60000.</comment>
<validate>required-entry validate-digits validate-zero-or-greater</validate>
<depends><field id="enabled">1</field></depends>
</field>
<field id="frequency" translate="label comment" type="select" sortOrder="80"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Display Frequency</label>
<comment>The display record is stored in the visitor's browser and isolated by Website.</comment>
<source_model>Esun\PromotionPopup\Model\Config\Source\Frequency</source_model>
<depends><field id="enabled">1</field></depends>
</field>
<field id="campaign_version" translate="label comment" type="text" sortOrder="90"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Campaign Version</label>
<comment>Increase this number when a changed campaign should be shown again to previous visitors.</comment>
<validate>required-entry validate-digits validate-greater-than-zero</validate>
<depends><field id="enabled">1</field></depends>
</field>
<field id="excluded_handles" translate="label comment" type="textarea" sortOrder="100"
showInDefault="1" showInWebsite="1" showInStore="0">
<label>Excluded Page Handles</label>
<comment>Enter one Magento full action name per line. The popup is never rendered on these pages.</comment>
<depends><field id="enabled">1</field></depends>
</field>
</group>
</section>
</system>
</config>
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<esun_promotion_popup>
<general>
<enabled>0</enabled>
<promotion_type>registration_newsletter_coupon</promotion_type>
<display_delay>1500</display_delay>
<frequency>daily</frequency>
<audience>guest</audience>
<campaign_version>1</campaign_version>
<excluded_handles>checkout_cart_index
checkout_index_index
customer_account_create
customer_account_login
customer_account_forgotpassword
customer_account_index
checkout_onepage_success</excluded_handles>
</general>
</esun_promotion_popup>
</default>
</config>
@@ -0,0 +1,13 @@
<?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="Esun_PromotionPopup">
<sequence>
<module name="Magento_Cms"/>
<module name="Magento_Config"/>
<module name="Magento_Customer"/>
<module name="Magento_Store"/>
<module name="Magento_Ui"/>
</sequence>
</module>
</config>
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Esun_PromotionPopup',
__DIR__
);
@@ -0,0 +1,14 @@
<?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="Esun_PromotionPopup::css/popup.css"/>
</head>
<body>
<referenceContainer name="before.body.end">
<block class="Esun\PromotionPopup\Block\Popup"
name="esun.promotion.popup"
template="Esun_PromotionPopup::popup.phtml"/>
</referenceContainer>
</body>
</page>
@@ -0,0 +1,27 @@
<?php
/**
* @var \Esun\PromotionPopup\Block\Popup $block
* @var \Magento\Framework\Escaper $escaper
*/
if (!$block->isAvailable()) {
return;
}
$content = $block->getCmsContent();
if (trim($content) === '') {
return;
}
?>
<div class="esun-promotion-popup"
data-role="esun-promotion-popup"
aria-hidden="true">
<?= /* @noEscape */ $content ?>
</div>
<script type="text/x-magento-init">
{
"[data-role=esun-promotion-popup]": {
"Esun_PromotionPopup/js/popup": <?= /* @noEscape */ $block->getJsonConfig() ?>
}
}
</script>
@@ -0,0 +1,239 @@
.modal-popup.esun-promotion-popup-modal .modal-inner-wrap {
background: transparent;
border-radius: 18px;
box-shadow: 0 24px 80px rgba(10, 45, 63, 0.28);
max-height: calc(100vh - 48px);
max-width: 1120px;
overflow: visible;
width: calc(100% - 48px);
}
.modal-popup.esun-promotion-popup-modal .modal-header {
height: 0;
padding: 0;
position: relative;
z-index: 5;
}
.modal-popup.esun-promotion-popup-modal .modal-title {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
.modal-popup.esun-promotion-popup-modal .action-close {
align-items: center;
background: rgba(15, 20, 22, 0.9);
border: 0;
border-radius: 50%;
color: #fff;
display: flex;
height: 48px;
justify-content: center;
padding: 0;
position: absolute;
right: 18px;
top: 18px;
transition: background-color 160ms ease, transform 160ms ease;
width: 48px;
}
.modal-popup.esun-promotion-popup-modal .action-close:hover,
.modal-popup.esun-promotion-popup-modal .action-close:focus {
background: #1e9e5a;
transform: scale(1.06);
}
.modal-popup.esun-promotion-popup-modal .modal-content {
background: #fff;
border-radius: 18px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 0;
}
.esun-promotion-popup {
display: none;
}
.esun-promotion-popup._show {
display: block;
}
.promotion-popup-content {
background: linear-gradient(135deg, #effff8 0%, #e4f7ff 100%);
border-radius: 18px;
min-height: 620px;
overflow: hidden;
position: relative;
}
.promotion-popup-content__media,
.promotion-popup-content__media picture {
display: block;
height: 100%;
inset: 0;
position: absolute;
width: 100%;
}
.promotion-popup-content__media img {
height: 100%;
object-fit: cover;
width: 100%;
}
.promotion-popup-content__body {
align-items: flex-start;
display: flex;
flex-direction: column;
justify-content: center;
min-height: 620px;
padding: 64px clamp(38px, 6vw, 84px);
position: relative;
width: 54%;
z-index: 1;
}
.promotion-popup-content__eyebrow {
color: #087d63;
font-size: 14px;
font-weight: 700;
letter-spacing: 0.14em;
margin: 0 0 14px;
}
.promotion-popup-content__title {
color: #102f3c;
font-size: clamp(48px, 5.2vw, 76px);
font-weight: 800;
letter-spacing: -0.04em;
line-height: 0.98;
margin: 0 0 18px;
}
.promotion-popup-content__condition {
color: #102f3c;
font-size: clamp(22px, 2.3vw, 32px);
font-weight: 700;
line-height: 1.15;
margin: 0 0 20px;
}
.promotion-popup-content__description {
color: #365866;
font-size: 17px;
line-height: 1.55;
margin: 0 0 28px;
max-width: 500px;
text-align: left;
}
.promotion-popup-content__button {
background: #159b64;
border-radius: 999px;
color: #fff !important;
display: inline-flex;
font-size: 18px;
font-weight: 700;
justify-content: center;
min-width: 230px;
padding: 16px 30px;
text-decoration: none !important;
transition: background-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}
.promotion-popup-content__button:hover,
.promotion-popup-content__button:focus {
background: #087d63;
box-shadow: 0 10px 26px rgba(8, 125, 99, 0.25);
transform: translateY(-2px);
}
.promotion-popup-content__terms {
color: #607a84;
font-size: 12px;
line-height: 1.45;
margin: 18px 0 0;
max-width: 460px;
text-align: left;
}
@media (max-width: 767px) {
.modal-popup.esun-promotion-popup-modal {
left: 0;
}
.modal-popup.esun-promotion-popup-modal .modal-inner-wrap {
border-radius: 14px;
margin: 16px auto;
max-height: calc(100vh - 32px);
width: calc(100% - 28px);
}
.modal-popup.esun-promotion-popup-modal .modal-content {
border-radius: 14px;
max-height: calc(100vh - 32px);
}
.modal-popup.esun-promotion-popup-modal .action-close {
height: 42px;
right: 12px;
top: 12px;
width: 42px;
}
.promotion-popup-content {
border-radius: 14px;
min-height: min(760px, calc(100vh - 32px));
}
.promotion-popup-content__body {
justify-content: flex-start;
min-height: min(760px, calc(100vh - 32px));
padding: 48px 28px 300px;
text-align: center;
width: auto;
}
.promotion-popup-content__eyebrow,
.promotion-popup-content__title,
.promotion-popup-content__condition,
.promotion-popup-content__description,
.promotion-popup-content__terms {
align-self: center;
text-align: left;
}
.promotion-popup-content__title {
font-size: clamp(42px, 13vw, 60px);
}
.promotion-popup-content__condition {
font-size: 23px;
}
.promotion-popup-content__description {
font-size: 15px;
margin-bottom: 22px;
text-align: left;
}
.promotion-popup-content__button {
align-self: center;
font-size: 16px;
min-width: 210px;
padding: 14px 24px;
}
}
@media (prefers-reduced-motion: reduce) {
.modal-popup.esun-promotion-popup-modal .action-close,
.promotion-popup-content__button {
transition: none;
}
}
@@ -0,0 +1,140 @@
define([
'jquery',
'Magento_Ui/js/modal/modal',
'Magento_Customer/js/customer-data',
'mage/translate'
], function ($, modal, customerData, $t) {
'use strict';
var DAY_IN_MILLISECONDS = 86400000;
return function (config, element) {
var $popup = $(element),
customer = customerData.get('customer'),
storageKey = [
'esunPromotionPopup',
String(config.websiteId),
String(config.campaignKey),
String(config.campaignVersion)
].join(':'),
timer = null,
opened = false;
function isLoggedIn() {
var data = customer() || {};
return Boolean(data.firstname || data.fullname || data.email);
}
function isCustomerAccountPage() {
return $('body').is('.account, .customer-account-index');
}
function readStorage(storage) {
try {
return storage.getItem(storageKey);
} catch (error) {
return null;
}
}
function writeStorage(storage, value) {
try {
storage.setItem(storageKey, value);
} catch (error) {
// Storage can be unavailable in private browsing; the popup should still work.
}
}
function wasAlreadyDisplayed() {
var value;
if (config.frequency === 'always') {
return false;
}
if (config.frequency === 'session') {
return Boolean(readStorage(window.sessionStorage));
}
value = readStorage(window.localStorage);
if (!value) {
return false;
}
return config.frequency === 'campaign' ||
Date.now() - parseInt(value, 10) < DAY_IN_MILLISECONDS;
}
function markDisplayed() {
var value = String(Date.now());
if (config.frequency === 'always') {
return;
}
if (config.frequency === 'session') {
writeStorage(window.sessionStorage, value);
return;
}
writeStorage(window.localStorage, value);
}
function isAudienceEligible() {
return config.audience !== 'guest' || !isLoggedIn();
}
function openPopup() {
if (opened || isCustomerAccountPage() || !isAudienceEligible() || wasAlreadyDisplayed()) {
return;
}
opened = true;
markDisplayed();
$popup.attr('aria-hidden', 'false').modal('openModal');
}
if (isCustomerAccountPage()) {
return;
}
modal({
type: 'popup',
responsive: true,
innerScroll: true,
modalClass: 'esun-promotion-popup-modal',
title: $t('Promotion'),
buttons: [],
clickableOverlay: true,
closed: function () {
$popup.attr('aria-hidden', 'true');
}
}, $popup);
function schedulePopup() {
if (!isAudienceEligible() || wasAlreadyDisplayed()) {
return;
}
timer = window.setTimeout(openPopup, Math.max(0, Number(config.displayDelay) || 0));
}
if (config.audience === 'guest') {
customer.subscribe(function () {
if (!isLoggedIn()) {
return;
}
window.clearTimeout(timer);
if (opened) {
$popup.modal('closeModal');
}
});
// Registration/login changes the server session before local customer-data is refreshed.
// Reload it before scheduling so authenticated customers never see a guest promotion.
customerData.reload(['customer'], false).always(schedulePopup);
return;
}
schedulePopup();
};
});