37 lines
1.0 KiB
PHP
37 lines
1.0 KiB
PHP
<?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;
|
|
}
|
|
}
|