PreventAppendReservationOnNotManageItemsInStockPlugin.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Plugin\InventoryReservationsApi;
  8. use Magento\CatalogInventory\Api\StockConfigurationInterface;
  9. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  10. use Magento\InventoryReservationsApi\Model\AppendReservationsInterface;
  11. use Magento\InventoryReservationsApi\Model\ReservationInterface;
  12. use Magento\Framework\Exception\LocalizedException;
  13. /**
  14. * Prevent append reservation if use_config_manage_stock is set to 0
  15. */
  16. class PreventAppendReservationOnNotManageItemsInStockPlugin
  17. {
  18. /**
  19. * @var GetStockItemConfigurationInterface
  20. */
  21. private $getStockItemConfiguration;
  22. /**
  23. * @var StockConfigurationInterface
  24. */
  25. private $stockConfiguration;
  26. /**
  27. * @param GetStockItemConfigurationInterface $getStockItemConfiguration
  28. * @param StockConfigurationInterface $stockConfiguration
  29. */
  30. public function __construct(
  31. GetStockItemConfigurationInterface $getStockItemConfiguration,
  32. StockConfigurationInterface $stockConfiguration
  33. ) {
  34. $this->getStockItemConfiguration = $getStockItemConfiguration;
  35. $this->stockConfiguration = $stockConfiguration;
  36. }
  37. /**
  38. * @param AppendReservationsInterface $subject
  39. * @param \Closure $proceed
  40. * @param ReservationInterface[] $reservations
  41. *
  42. * @throws LocalizedException
  43. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  44. */
  45. public function aroundExecute(AppendReservationsInterface $subject, \Closure $proceed, array $reservations)
  46. {
  47. if (!$this->stockConfiguration->canSubtractQty()) {
  48. return;
  49. }
  50. $reservationToAppend = [];
  51. foreach ($reservations as $reservation) {
  52. $stockItemConfiguration = $this->getStockItemConfiguration->execute(
  53. $reservation->getSku(),
  54. $reservation->getStockId()
  55. );
  56. if ($stockItemConfiguration->isManageStock()) {
  57. $reservationToAppend[] = $reservation;
  58. }
  59. }
  60. if (!empty($reservationToAppend)) {
  61. $proceed($reservationToAppend);
  62. }
  63. }
  64. }