init
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FreeGift\Plugin\Quote;
|
||||
|
||||
use Esun\FreeGift\Model\GiftItem;
|
||||
use Esun\FreeGift\Model\GiftSynchronizer;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\Quote\Address\Total;
|
||||
use Magento\Quote\Model\Quote\TotalsCollector;
|
||||
|
||||
class TotalsCollectorPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GiftSynchronizer $giftSynchronizer
|
||||
) {
|
||||
}
|
||||
|
||||
public function aroundCollect(
|
||||
TotalsCollector $subject,
|
||||
callable $proceed,
|
||||
Quote $quote
|
||||
): Total {
|
||||
if ((bool)$quote->getData(GiftItem::TOTALS_GUARD)) {
|
||||
return $proceed($quote);
|
||||
}
|
||||
|
||||
$quote->setData(GiftItem::TOTALS_GUARD, 1);
|
||||
|
||||
try {
|
||||
/** @var Total $total */
|
||||
$total = $proceed($quote);
|
||||
$eligibleBaseSubtotal = (float)$total->getBaseSubtotalWithDiscount();
|
||||
|
||||
if ($this->giftSynchronizer->synchronize($quote, $eligibleBaseSubtotal)) {
|
||||
/** @var Total $total */
|
||||
$total = $proceed($quote);
|
||||
}
|
||||
|
||||
return $total;
|
||||
} finally {
|
||||
$quote->unsetData(GiftItem::TOTALS_GUARD);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user