diff --git a/src/Resources/config/services/services.xml b/src/Resources/config/services/services.xml index 0a77f60ad..7b670774b 100644 --- a/src/Resources/config/services/services.xml +++ b/src/Resources/config/services/services.xml @@ -79,6 +79,7 @@ + diff --git a/src/Service/Stock/StockManager.php b/src/Service/Stock/StockManager.php index c7e0673b1..84d7316c9 100644 --- a/src/Service/Stock/StockManager.php +++ b/src/Service/Stock/StockManager.php @@ -8,39 +8,61 @@ use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity; use Shopware\Core\Defaults; use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery; +use Shopware\Core\Framework\Feature; use Shopware\Core\Framework\Uuid\Uuid; +use Symfony\Component\DependencyInjection\ContainerInterface; class StockManager implements StockManagerInterface { + private const STOCK_MANAGER_PARAMETER_NAME = 'shopware.stock.enable_stock_management'; /** * @var Connection */ private $connection; + /** + * @var bool + */ + private $enableStockManagement; + /** * @param Connection $connection + * @param ContainerInterface $container */ - public function __construct(Connection $connection) + public function __construct(Connection $connection, ContainerInterface $container) { $this->connection = $connection; + $this->enableStockManagement = false; + + /** + * We have to use here the container because the parameter does not exists in earlier shopware versions and we get an exceptions + * when activating the plugin + */ + if ($container->hasParameter(self::STOCK_MANAGER_PARAMETER_NAME)) { + $this->enableStockManagement = (bool)$container->getParameter(self::STOCK_MANAGER_PARAMETER_NAME); + } } /** * @param OrderLineItemEntity $lineItem * @param int $quantity * @param string $mollieRefundID - * @throws \Doctrine\DBAL\Exception * @return void + * @throws \Doctrine\DBAL\Exception */ public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, string $mollieRefundID): void { + if ($this->isEnabled() === false) { + return; + } + if ($lineItem->getPayload() === null) { return; } # check if we have a product - if (!isset($lineItem->getPayload()['productNumber'])) { + if (! isset($lineItem->getPayload()['productNumber'])) { return; } @@ -59,4 +81,9 @@ public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, stri ] ); } + + private function isEnabled(): bool + { + return $this->enableStockManagement && Feature::isActive('STOCK_HANDLING'); + } }