init
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerCoupon\ViewModel;
|
||||
|
||||
use Esun\CustomerCoupon\Model\Config as CouponConfig;
|
||||
use Esun\CustomerCoupon\Model\Coupon\DiscountFormatter;
|
||||
use Esun\PromotionPopup\Model\Config as PromotionConfig;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\SalesRule\Model\Rule;
|
||||
use Magento\SalesRule\Model\RuleFactory;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class RegistrationSubscriptionPromotion implements ArgumentInterface
|
||||
{
|
||||
private const XML_PATH_DEFAULT_CUSTOMER_GROUP = 'customer/create_account/default_group';
|
||||
|
||||
private bool $ruleResolved = false;
|
||||
private ?Rule $eligibleRule = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly CouponConfig $couponConfig,
|
||||
private readonly PromotionConfig $promotionConfig,
|
||||
private readonly RuleFactory $ruleFactory,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly TimezoneInterface $timezone,
|
||||
private readonly ScopeConfigInterface $scopeConfig,
|
||||
private readonly DiscountFormatter $discountFormatter
|
||||
) {
|
||||
}
|
||||
|
||||
public function isReminderEnabled(): bool
|
||||
{
|
||||
return $this->getEligibleRule() !== null;
|
||||
}
|
||||
|
||||
public function getReminderMessage(): string
|
||||
{
|
||||
$rule = $this->getEligibleRule();
|
||||
|
||||
return $rule
|
||||
? (string)__('Subscribe now to receive your %1 coupon.', $this->discountFormatter->format($rule))
|
||||
: '';
|
||||
}
|
||||
|
||||
public function getDiscountLabel(): string
|
||||
{
|
||||
$rule = $this->getEligibleRule();
|
||||
|
||||
return $rule ? $this->discountFormatter->format($rule) : '';
|
||||
}
|
||||
|
||||
private function getEligibleRule(): ?Rule
|
||||
{
|
||||
if ($this->ruleResolved) {
|
||||
return $this->eligibleRule;
|
||||
}
|
||||
$this->ruleResolved = true;
|
||||
|
||||
if (!$this->promotionConfig->isCampaignActive()
|
||||
|| !$this->promotionConfig->isRegistrationNewsletterCouponPromotion()
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$store = $this->storeManager->getStore();
|
||||
$ruleId = $this->couponConfig->getRegistrationSubscriptionRuleId((int)$store->getId());
|
||||
if ($ruleId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Rule $rule */
|
||||
$rule = $this->ruleFactory->create()->load($ruleId);
|
||||
$websiteId = (int)$store->getWebsiteId();
|
||||
$defaultGroupId = (int)$this->scopeConfig->getValue(
|
||||
self::XML_PATH_DEFAULT_CUSTOMER_GROUP,
|
||||
ScopeInterface::SCOPE_STORE,
|
||||
(int)$store->getId()
|
||||
);
|
||||
|
||||
$eligible = (bool)$rule->getId()
|
||||
&& (bool)$rule->getIsActive()
|
||||
&& ((int)$rule->getCouponType() === Rule::COUPON_TYPE_AUTO || (bool)$rule->getUseAutoGeneration())
|
||||
&& in_array($websiteId, array_map('intval', $rule->getWebsiteIds()), true)
|
||||
&& in_array($defaultGroupId, array_map('intval', $rule->getCustomerGroupIds()), true)
|
||||
&& $this->timezone->isScopeDateInInterval($store, $rule->getFromDate(), $rule->getToDate());
|
||||
|
||||
$this->eligibleRule = $eligible ? $rule : null;
|
||||
|
||||
return $this->eligibleRule;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user