init
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FreeGift\Plugin\Quote;
|
||||
|
||||
use Esun\FreeGift\Model\Config;
|
||||
use Esun\FreeGift\Model\InternalAddGuard;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Quote\Model\Quote;
|
||||
|
||||
class AddProductPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly InternalAddGuard $internalAddGuard
|
||||
) {
|
||||
}
|
||||
|
||||
public function beforeAddProduct(
|
||||
Quote $subject,
|
||||
Product $product,
|
||||
mixed $request = null,
|
||||
string $processMode = \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL
|
||||
): ?array {
|
||||
$storeId = (int)($subject->getStoreId() ?: $product->getStoreId());
|
||||
|
||||
if ($this->config->isGiftSku((string)$product->getSku(), $storeId)
|
||||
&& !$this->internalAddGuard->isAllowed()
|
||||
) {
|
||||
throw new LocalizedException(__('This promotional gift cannot be purchased separately.'));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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