Skip to content

Commit 331b5c1

Browse files
Shikha Mishrairajneeshgupta
Shikha Mishra
authored andcommitted
Updated CancelOrderItemObserver.php
Fixed Issue #20121 Cancel order increases stock although "Set Items' Status to be In Stock When Order is Cancelled" is set to No
1 parent 8fdae10 commit 331b5c1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

app/code/Magento/CatalogInventory/Observer/CancelOrderItemObserver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\CatalogInventory\Observer;
88

9+
use Magento\CatalogInventory\Model\Configuration;
910
use Magento\Framework\Event\ObserverInterface;
1011
use Magento\CatalogInventory\Api\StockManagementInterface;
1112
use Magento\Framework\Event\Observer as EventObserver;
@@ -15,6 +16,11 @@
1516
*/
1617
class CancelOrderItemObserver implements ObserverInterface
1718
{
19+
/**
20+
* @var \Magento\CatalogInventory\Model\Configuration
21+
*/
22+
protected $configuration;
23+
1824
/**
1925
* @var StockManagementInterface
2026
*/
@@ -30,9 +36,11 @@ class CancelOrderItemObserver implements ObserverInterface
3036
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer
3137
*/
3238
public function __construct(
39+
Configuration $configuration,
3340
StockManagementInterface $stockManagement,
3441
\Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer
3542
) {
43+
$this->configuration = $configuration;
3644
$this->stockManagement = $stockManagement;
3745
$this->priceIndexer = $priceIndexer;
3846
}
@@ -49,7 +57,8 @@ public function execute(EventObserver $observer)
4957
$item = $observer->getEvent()->getItem();
5058
$children = $item->getChildrenItems();
5159
$qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled();
52-
if ($item->getId() && $item->getProductId() && empty($children) && $qty) {
60+
if ($item->getId() && $item->getProductId() && empty($children) && $qty && $this->configuration
61+
->getCanBackInStock()) {
5362
$this->stockManagement->backItemQty($item->getProductId(), $qty, $item->getStore()->getWebsiteId());
5463
}
5564
$this->priceIndexer->reindexRow($item->getProductId());

0 commit comments

Comments
 (0)