20 lines
951 B
PHP
20 lines
951 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Reward\Observer;
|
|
|
|
use Esun\Reward\Model\ReservationManager;
|
|
use Magento\Framework\Event\Observer;
|
|
use Magento\Framework\Event\ObserverInterface;
|
|
|
|
class RecalculateRedemptionPoints implements ObserverInterface
|
|
{
|
|
public function __construct(private readonly ReservationManager $reservations) {}
|
|
public function execute(Observer $observer): void
|
|
{
|
|
$cart=$observer->getEvent()->getCart();$quote=$cart?->getQuote();if(!$quote||!(int)$quote->getCustomerId()){return;}$points=0;
|
|
foreach($quote->getAllVisibleItems() as $item){if((int)$item->getData('reward_redemption')){$points+=(int)$item->getData('reward_points_cost')*(int)$item->getQty();}}
|
|
$quote->setData('reward_product_points',$points);$this->reservations->reserve((int)$quote->getCustomerId(),(int)$quote->getStore()->getWebsiteId(),(int)$quote->getId(),$points+(int)$quote->getData('reward_points_used'));
|
|
}
|
|
}
|