Files
esun3d/app/code/Esun/PromotionPopup/Block/Popup.php
T
2026-09-15 10:35:56 +08:00

58 lines
1.6 KiB
PHP

<?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(),
]);
}
}