UpdateItemsStockUponConfigChangeObserver.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Framework\Event\Observer as EventObserver;
  8. use Magento\Framework\Event\ObserverInterface;
  9. use Magento\CatalogInventory\Model\Configuration;
  10. use Magento\CatalogInventory\Model\ResourceModel\Stock\Item;
  11. /**
  12. * Catalog inventory module observer
  13. */
  14. class UpdateItemsStockUponConfigChangeObserver implements ObserverInterface
  15. {
  16. /**
  17. * @var Item
  18. */
  19. protected $resourceStockItem;
  20. /**
  21. * @param Item $resourceStockItem
  22. */
  23. public function __construct(Item $resourceStockItem)
  24. {
  25. $this->resourceStockItem = $resourceStockItem;
  26. }
  27. /**
  28. * Update items stock status and low stock date.
  29. *
  30. * @param EventObserver $observer
  31. * @return void
  32. */
  33. public function execute(EventObserver $observer)
  34. {
  35. $website = (int) $observer->getEvent()->getWebsite();
  36. $changedPaths = (array) $observer->getEvent()->getChangedPaths();
  37. if (\array_intersect([
  38. Configuration::XML_PATH_MANAGE_STOCK,
  39. Configuration::XML_PATH_MIN_QTY,
  40. Configuration::XML_PATH_BACKORDERS,
  41. Configuration::XML_PATH_NOTIFY_STOCK_QTY,
  42. ], $changedPaths)) {
  43. $this->resourceStockItem->updateSetOutOfStock($website);
  44. $this->resourceStockItem->updateSetInStock($website);
  45. $this->resourceStockItem->updateLowStockDate($website);
  46. }
  47. }
  48. }