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

[Backport] Fixed Issue #20121 Cancel order increases stock although "Set Items' Status to be In Stock When Order is Cancelled" is set to No #20547

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,15 +6,21 @@

namespace Magento\CatalogInventory\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\CatalogInventory\Api\StockManagementInterface;
use Magento\CatalogInventory\Model\Configuration;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

/**
* Catalog inventory module observer
*/
class CancelOrderItemObserver implements ObserverInterface
{
/**
* @var \Magento\CatalogInventory\Model\Configuration
*/
protected $configuration;

/**
* @var StockManagementInterface
*/
Expand All @@ -26,13 +32,16 @@ class CancelOrderItemObserver implements ObserverInterface
protected $priceIndexer;

/**
* @param Configuration $configuration
* @param StockManagementInterface $stockManagement
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer
*/
public function __construct(
Configuration $configuration,
StockManagementInterface $stockManagement,
\Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer
) {
$this->configuration = $configuration;
$this->stockManagement = $stockManagement;
$this->priceIndexer = $priceIndexer;
}
Expand All @@ -49,7 +58,8 @@ public function execute(EventObserver $observer)
$item = $observer->getEvent()->getItem();
$children = $item->getChildrenItems();
$qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled();
if ($item->getId() && $item->getProductId() && empty($children) && $qty) {
if ($item->getId() && $item->getProductId() && empty($children) && $qty && $this->configuration
->getCanBackInStock()) {
$this->stockManagement->backItemQty($item->getProductId(), $qty, $item->getStore()->getWebsiteId());
}
$this->priceIndexer->reindexRow($item->getProductId());
Expand Down