59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?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;
|
|
}
|
|
}
|