1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Model\Observer; |
5
|
|
|
|
6
|
|
|
use Magento\CatalogInventory\Observer\ProductQty; |
7
|
|
|
use Magento\CatalogInventory\Api\StockManagementInterface; |
8
|
|
|
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as IndexStockProcessor; |
9
|
|
|
use Magento\Catalog\Model\Indexer\Product\Price\Processor as ProductPriceProcessor; |
10
|
|
|
use Magento\Framework\Event\Observer as EventObserver; |
11
|
|
|
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RevertQuoteInventoryObserver |
15
|
|
|
*/ |
16
|
|
|
class RevertQuoteInventoryObserver extends \Magento\CatalogInventory\Observer\RevertQuoteInventoryObserver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var StockbaseStockManagement |
20
|
|
|
*/ |
21
|
|
|
private $stockbaseStockManagement; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RevertQuoteInventoryObserver constructor. |
25
|
|
|
* @param ProductQty $productQty |
26
|
|
|
* @param StockManagementInterface $stockManagement |
27
|
|
|
* @param IndexStockProcessor $stockIndexerProcessor |
28
|
|
|
* @param ProductPriceProcessor $priceIndexer |
29
|
|
|
* @param StockbaseStockManagement $stockbaseStockManagement |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct( |
32
|
|
|
ProductQty $productQty, |
33
|
|
|
StockManagementInterface $stockManagement, |
34
|
|
|
IndexStockProcessor $stockIndexerProcessor, |
35
|
|
|
ProductPriceProcessor $priceIndexer, |
36
|
|
|
StockbaseStockManagement $stockbaseStockManagement |
37
|
|
|
) { |
38
|
3 |
|
parent::__construct($productQty, $stockManagement, $stockIndexerProcessor, $priceIndexer); |
39
|
3 |
|
$this->stockbaseStockManagement = $stockbaseStockManagement; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
3 |
|
public function execute(EventObserver $observer) |
46
|
|
|
{ |
47
|
|
|
/** @var \Magento\Quote\Model\Quote $quote */ |
48
|
3 |
|
$quote = $observer->getEvent()->getQuote(); |
49
|
|
|
|
50
|
3 |
|
$items = []; |
51
|
3 |
|
foreach ($quote->getAllItems() as $item) { |
52
|
2 |
|
$productId = $item->getProductId(); |
53
|
2 |
|
if (!$productId) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
2 |
|
$children = $item->getChildrenItems() ?: [$item]; |
57
|
2 |
|
foreach ($children as $childItem) { |
58
|
2 |
|
$productId = $childItem->getProductId(); |
59
|
2 |
|
if (!$productId) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$amount = $childItem->getTotalQty(); |
64
|
2 |
|
$reserve = $this->stockbaseStockManagement->getReserveForQuoteItem($childItem->getId()); |
65
|
2 |
View Code Duplication |
if (!empty($reserve)) { |
|
|
|
|
66
|
2 |
|
$reserve = reset($reserve); |
67
|
2 |
|
$amount -= $reserve->getAmount(); |
68
|
|
|
//if ($amount != $reserve->getMagentoStockAmount()) { |
69
|
|
|
// throw new \Exception('Invalid quote inventory revert.'); //TODO: Enable additional checks |
70
|
|
|
//} |
71
|
2 |
|
$this->stockbaseStockManagement->releaseReserve($reserve); |
|
|
|
|
72
|
|
|
} |
73
|
2 |
|
if (!isset($items[$productId])) { |
74
|
2 |
|
$items[$productId] = 0; |
75
|
|
|
} |
76
|
2 |
|
$items[$productId] += $amount; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
$this->stockManagement->revertProductsSale($items, $quote->getStore()->getWebsiteId()); |
81
|
3 |
|
$productIds = array_keys($items); |
82
|
3 |
|
if (!empty($productIds)) { |
83
|
2 |
|
$this->stockIndexerProcessor->reindexList($productIds); |
84
|
2 |
|
$this->priceIndexer->reindexList($productIds); |
85
|
|
|
} |
86
|
|
|
// Clear flag, so if order placement retried again with success - it will be processed |
87
|
3 |
|
$quote->setInventoryProcessed(false); |
88
|
3 |
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.