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,58 @@
<?php
declare(strict_types=1);
namespace Esun\FreeGift\Plugin\CustomerData;
use Esun\FreeGift\Model\Config;
use Esun\FreeGift\Model\GiftItem;
use Magento\Checkout\CustomerData\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
class CartPlugin
{
public function __construct(
private readonly CheckoutSession $checkoutSession,
private readonly Config $config
) {
}
public function afterGetSectionData(Cart $subject, array $result): array
{
$giftItemIds = [];
$quote = $this->checkoutSession->getQuote();
$storeId = (int)$quote->getStoreId();
foreach ($quote->getAllVisibleItems() as $quoteItem) {
if ((bool)$quoteItem->getData(GiftItem::FLAG)
|| $this->config->isGiftSku((string)$quoteItem->getSku(), $storeId)
) {
$giftItemIds[(int)$quoteItem->getId()] = true;
}
}
if (!isset($result['items']) || !is_array($result['items'])) {
return $result;
}
foreach ($result['items'] as &$item) {
$isGift = (bool)($item['is_esun_free_gift'] ?? false)
|| isset($giftItemIds[(int)($item['item_id'] ?? 0)])
|| $this->config->isGiftSku((string)($item['product_sku'] ?? ''), $storeId);
$item['is_esun_free_gift'] = $isGift;
if ($isGift) {
$item['line_total'] = (string)__('Free');
$item['original_price'] = '';
$item['product_price'] = (string)__('Free');
$item['product_price_value'] = 0;
$item['product_url'] = '';
$item['product_has_url'] = false;
$item['configure_url'] = '';
}
}
unset($item);
return $result;
}
}
@@ -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);
}
}
}