Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOL-1285: use stock manager config #669

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Resources/config/services/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

<service id="Kiener\MolliePayments\Service\Stock\StockManager">
<argument type="service" id="Doctrine\DBAL\Connection"/>
<argument type="service" id="service_container"/>
</service>


Expand Down
40 changes: 38 additions & 2 deletions src/Service/Stock/StockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,44 @@
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;

/**
* Enable stock management per default and disable it by config
*/
$this->enableStockManagement = true;

/**
* 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);
}
}

/**
Expand All @@ -35,12 +57,16 @@ public function __construct(Connection $connection)
*/
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;
}

Expand All @@ -59,4 +85,14 @@ public function increaseStock(OrderLineItemEntity $lineItem, int $quantity, stri
]
);
}

/**
* We do not listen to Feature::isActive('STOCK_HANDLING') this feature is disabled by default and enabled in later versions
* if we listen to this feature, we will introduce breaking changes and this feature has to be enabled explicit so the refund manager will increase the stock
* @return bool
*/
private function isEnabled(): bool
{
return $this->enableStockManagement;
}
}
Loading