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,40 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerCoupon\Observer;
use Esun\CustomerCoupon\Block\Adminhtml\Form\CustomerSelect;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\View\LayoutInterface;
class AddCustomerSelectToCouponForm implements ObserverInterface
{
public function __construct(private readonly LayoutInterface $layout)
{
}
public function execute(Observer $observer): void
{
$form = $observer->getData('form');
$fieldset = $form ? $form->getElement('information_fieldset') : null;
if (!$fieldset || $form->getElement('customer_ids')) {
return;
}
$html = $this->layout->createBlock(CustomerSelect::class)
->setTemplate('Esun_CustomerCoupon::form/customer-select.phtml')
->toHtml();
$fieldset->addField(
'customer_ids',
'note',
[
'label' => __('Assigned Customers'),
'text' => $html,
'note' => __('When customers are selected, one unique coupon is generated for each customer.'),
],
'dash'
);
}
}
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerCoupon\Observer;
use Esun\CustomerCoupon\Model\RegistrationSubscriptionCouponService;
use Esun\CustomerCoupon\Model\RegistrationCouponGrantContext;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
class GrantRegistrationSubscriptionCoupon implements ObserverInterface
{
public function __construct(
private readonly RegistrationSubscriptionCouponService $couponService,
private readonly RegistrationCouponGrantContext $grantContext,
private readonly LoggerInterface $logger
) {
}
public function execute(Observer $observer): void
{
$controller = $observer->getEvent()->getAccountController();
$subscriptionRequested = $controller?->getRequest()->getParam('is_subscribed', false);
if (!in_array(strtolower((string)$subscriptionRequested), ['1', 'true', 'yes', 'on'], true)) {
return;
}
$customer = $observer->getEvent()->getCustomer();
$customerId = (int)($customer?->getId() ?? 0);
if ($customerId <= 0) {
return;
}
try {
$granted = $this->couponService->grant(
$customerId,
(int)$customer->getWebsiteId(),
(int)$customer->getGroupId(),
(int)$customer->getStoreId()
);
if ($granted) {
$this->grantContext->markGranted($customerId);
}
} catch (\Throwable $exception) {
// Coupon delivery must never roll back an otherwise successful customer registration.
$this->logger->error('Unable to grant registration subscription coupon.', [
'customer_id' => $customerId,
'exception' => $exception,
]);
}
}
}