ProcessInventoryDataObserver.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Api\Data\StockItemInterface;
  9. use Magento\CatalogInventory\Model\Stock\Item;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\Framework\Event\Observer as EventObserver;
  12. /**
  13. * Prepares stock data for saving
  14. *
  15. * @deprecated 100.2.0 Stock data should be processed using the module API
  16. * @see StockItemInterface when you want to change the stock data
  17. * @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
  18. * @see StockItemRepositoryInterface::save as extension point for customization of saving process
  19. */
  20. class ProcessInventoryDataObserver implements ObserverInterface
  21. {
  22. /**
  23. * Stock Registry
  24. *
  25. * @var \Magento\CatalogInventory\Api\StockRegistryInterface
  26. */
  27. private $stockRegistry;
  28. /**
  29. * Construct
  30. *
  31. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  32. */
  33. public function __construct(
  34. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  35. ) {
  36. $this->stockRegistry = $stockRegistry;
  37. }
  38. /**
  39. * Process stock item data
  40. *
  41. * @param EventObserver $observer
  42. * @return void
  43. */
  44. public function execute(EventObserver $observer)
  45. {
  46. $product = $observer->getEvent()->getProduct();
  47. $this->processStockData($product);
  48. }
  49. /**
  50. * Process stock item data
  51. *
  52. * Synchronize stock data from different sources (stock_data, quantity_and_stock_status, StockItem) and set it to
  53. * stock_data key
  54. *
  55. * @param Product $product
  56. * @return void
  57. */
  58. private function processStockData(Product $product)
  59. {
  60. $quantityAndStockStatus = $product->getData('quantity_and_stock_status');
  61. if (is_array($quantityAndStockStatus)) {
  62. /** @var Item $stockItem */
  63. $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
  64. $quantityAndStockStatus = $this->prepareQuantityAndStockStatus($stockItem, $quantityAndStockStatus);
  65. if ($quantityAndStockStatus) {
  66. $this->setStockDataToProduct($product, $stockItem, $quantityAndStockStatus);
  67. }
  68. }
  69. }
  70. /**
  71. * Prepare quantity_and_stock_status data
  72. *
  73. * Remove not changed values from quantity_and_stock_status data
  74. * Set null value for qty if passed empty
  75. *
  76. * @param StockItemInterface $stockItem
  77. * @param array $quantityAndStockStatus
  78. * @return array
  79. */
  80. private function prepareQuantityAndStockStatus(StockItemInterface $stockItem, array $quantityAndStockStatus)
  81. {
  82. $stockItemId = $stockItem->getItemId();
  83. if (null !== $stockItemId) {
  84. if (isset($quantityAndStockStatus['is_in_stock'])
  85. && $stockItem->getIsInStock() == $quantityAndStockStatus['is_in_stock']
  86. ) {
  87. unset($quantityAndStockStatus['is_in_stock']);
  88. }
  89. if (array_key_exists('qty', $quantityAndStockStatus)
  90. && $stockItem->getQty() == $quantityAndStockStatus['qty']
  91. ) {
  92. unset($quantityAndStockStatus['qty']);
  93. }
  94. }
  95. if (array_key_exists('qty', $quantityAndStockStatus) && $quantityAndStockStatus['qty'] === '') {
  96. $quantityAndStockStatus['qty'] = null;
  97. }
  98. return $quantityAndStockStatus;
  99. }
  100. /**
  101. * Set stock data to product
  102. *
  103. * First of all we take stock_data data, replace it from quantity_and_stock_status data (if was changed) and finally
  104. * replace it with data from Stock Item object (only if Stock Item was changed)
  105. *
  106. * @param Product $product
  107. * @param Item $stockItem
  108. * @param array $quantityAndStockStatus
  109. * @return void
  110. */
  111. private function setStockDataToProduct(Product $product, Item $stockItem, array $quantityAndStockStatus)
  112. {
  113. $stockData = array_replace((array)$product->getData('stock_data'), $quantityAndStockStatus);
  114. if ($stockItem->hasDataChanges()) {
  115. $stockData = array_replace($stockData, $stockItem->getData());
  116. }
  117. $product->setData('stock_data', $stockData);
  118. }
  119. }