Files
esun3d/app/code/Esun/Reward/Observer/AwardRegistrationPoints.php
2026-09-15 10:35:56 +08:00

27 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Reward\Observer;
use Esun\Reward\Model\BalanceManager;
use Esun\Reward\Model\Config;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class AwardRegistrationPoints implements ObserverInterface
{
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config) {}
public function execute(Observer $observer): void
{
$customer = $observer->getEvent()->getCustomer();
$customerId = (int)($customer?->getId() ?? 0);
$websiteId = (int)($customer?->getWebsiteId() ?? 0);
if (!$this->config->isEnabled() || $customerId <= 0) { return; }
$rule = $this->config->earningRule('registration');
$this->balanceManager->change($customerId, $websiteId, (int)$rule['points'], 'registration', 'registration:' . $customerId, [
'reference_type' => 'customer', 'reference_id' => $customerId, 'reason' => (string)__($rule['reason'])
]);
}
}