SaveInventoryDataObserver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Observer;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\CatalogInventory\Model\Stock\Item;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\CatalogInventory\Api\StockConfigurationInterface;
  12. use Magento\CatalogInventory\Api\StockRegistryInterface;
  13. use Magento\CatalogInventory\Model\StockItemValidator;
  14. use Magento\Framework\Event\Observer as EventObserver;
  15. /**
  16. * Saves stock data from a product to the Stock Item
  17. *
  18. * @deprecated 100.2.0 Stock data should be processed using the module API
  19. * @see StockItemInterface when you want to change the stock data
  20. * @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
  21. * @see StockItemRepositoryInterface::save as extension point for customization of saving process
  22. */
  23. class SaveInventoryDataObserver implements ObserverInterface
  24. {
  25. /**
  26. * @var StockConfigurationInterface
  27. */
  28. private $stockConfiguration;
  29. /**
  30. * @var StockRegistryInterface
  31. */
  32. private $stockRegistry;
  33. /**
  34. * @var StockItemValidator
  35. */
  36. private $stockItemValidator;
  37. /**
  38. * @var array
  39. */
  40. private $paramListToCheck = [
  41. 'use_config_min_qty' => [
  42. 'item' => 'stock_data/min_qty',
  43. 'config' => 'stock_data/use_config_min_qty',
  44. ],
  45. 'use_config_min_sale_qty' => [
  46. 'item' => 'stock_data/min_sale_qty',
  47. 'config' => 'stock_data/use_config_min_sale_qty',
  48. ],
  49. 'use_config_max_sale_qty' => [
  50. 'item' => 'stock_data/max_sale_qty',
  51. 'config' => 'stock_data/use_config_max_sale_qty',
  52. ],
  53. 'use_config_backorders' => [
  54. 'item' => 'stock_data/backorders',
  55. 'config' => 'stock_data/use_config_backorders',
  56. ],
  57. 'use_config_notify_stock_qty' => [
  58. 'item' => 'stock_data/notify_stock_qty',
  59. 'config' => 'stock_data/use_config_notify_stock_qty',
  60. ],
  61. 'use_config_enable_qty_inc' => [
  62. 'item' => 'stock_data/enable_qty_increments',
  63. 'config' => 'stock_data/use_config_enable_qty_inc',
  64. ],
  65. 'use_config_qty_increments' => [
  66. 'item' => 'stock_data/qty_increments',
  67. 'config' => 'stock_data/use_config_qty_increments',
  68. ],
  69. ];
  70. /**
  71. * @param StockConfigurationInterface $stockConfiguration
  72. * @param StockRegistryInterface $stockRegistry
  73. * @param StockItemValidator $stockItemValidator
  74. */
  75. public function __construct(
  76. StockConfigurationInterface $stockConfiguration,
  77. StockRegistryInterface $stockRegistry,
  78. StockItemValidator $stockItemValidator = null
  79. ) {
  80. $this->stockConfiguration = $stockConfiguration;
  81. $this->stockRegistry = $stockRegistry;
  82. $this->stockItemValidator = $stockItemValidator ?: ObjectManager::getInstance()->get(StockItemValidator::class);
  83. }
  84. /**
  85. * Saving product inventory data
  86. *
  87. * Takes data from the stock_data property of a product and sets it to Stock Item.
  88. * Validates and saves Stock Item object.
  89. *
  90. * @param EventObserver $observer
  91. * @return void
  92. */
  93. public function execute(EventObserver $observer)
  94. {
  95. $product = $observer->getEvent()->getProduct();
  96. $stockItem = $this->getStockItemToBeUpdated($product);
  97. if ($product->getStockData() !== null) {
  98. $stockData = $this->getStockData($product);
  99. $stockItem->addData($stockData);
  100. }
  101. $this->stockItemValidator->validate($product, $stockItem);
  102. $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
  103. }
  104. /**
  105. * Return the stock item that needs to be updated
  106. *
  107. * @param Product $product
  108. * @return Item
  109. */
  110. private function getStockItemToBeUpdated(Product $product)
  111. {
  112. $extendedAttributes = $product->getExtensionAttributes();
  113. $stockItem = $extendedAttributes->getStockItem();
  114. if ($stockItem === null) {
  115. $stockItem = $this->stockRegistry->getStockItem($product->getId());
  116. }
  117. return $stockItem;
  118. }
  119. /**
  120. * Get stock data
  121. *
  122. * @param Product $product
  123. * @return array
  124. */
  125. private function getStockData(Product $product)
  126. {
  127. $stockData = $product->getStockData();
  128. $stockData['product_id'] = $product->getId();
  129. if (!isset($stockData['website_id'])) {
  130. $stockData['website_id'] = $this->stockConfiguration->getDefaultScopeId();
  131. }
  132. $stockData['stock_id'] = $this->stockRegistry->getStock($stockData['website_id'])->getStockId();
  133. foreach ($this->paramListToCheck as $dataKey => $configPath) {
  134. if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) {
  135. $stockData[$dataKey] = false;
  136. }
  137. }
  138. $originalQty = $product->getData('stock_data/original_inventory_qty');
  139. if (strlen($originalQty) > 0) {
  140. $stockData['qty_correction'] = (isset($stockData['qty']) ? $stockData['qty'] : 0)
  141. - $originalQty;
  142. }
  143. return $stockData;
  144. }
  145. }