ProcessSourceItemsObserver.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\InventoryConfigurableProductAdminUi\Observer;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Controller\Adminhtml\Product\Save;
  10. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  11. use Magento\Framework\Event\Observer as EventObserver;
  12. use Magento\Framework\Event\ObserverInterface;
  13. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  14. use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
  15. use Magento\InventoryCatalogAdminUi\Observer\SourceItemsProcessor;
  16. /**
  17. * Process source items for configurable children products during product saving via controller.
  18. */
  19. class ProcessSourceItemsObserver implements ObserverInterface
  20. {
  21. /**
  22. * @var SourceItemsProcessor
  23. */
  24. private $sourceItemsProcessor;
  25. /**
  26. * @var GetSkusByProductIdsInterface
  27. */
  28. private $getSkusByProductIdsInterface;
  29. /**
  30. * @param SourceItemsProcessor $sourceItemsProcessor
  31. * @param GetSkusByProductIdsInterface $getSkusByProductIdsInterface
  32. */
  33. public function __construct(
  34. SourceItemsProcessor $sourceItemsProcessor,
  35. GetSkusByProductIdsInterface $getSkusByProductIdsInterface
  36. ) {
  37. $this->sourceItemsProcessor = $sourceItemsProcessor;
  38. $this->getSkusByProductIdsInterface = $getSkusByProductIdsInterface;
  39. }
  40. /**
  41. * @param EventObserver $observer
  42. *
  43. * @return void
  44. */
  45. public function execute(EventObserver $observer)
  46. {
  47. /** @var ProductInterface $product */
  48. $product = $observer->getEvent()->getProduct();
  49. if ($product->getTypeId() !== Configurable::TYPE_CODE) {
  50. return;
  51. }
  52. /** @var Save $controller */
  53. $controller = $observer->getEvent()->getController();
  54. $configurableMatrix = $controller->getRequest()->getParam('configurable-matrix-serialized', '');
  55. if ($configurableMatrix != '') {
  56. $productsData = json_decode($configurableMatrix, true);
  57. foreach ($productsData as $key => $productData) {
  58. if (isset($productData['quantity_per_source'])) {
  59. $quantityPerSource = is_array($productData['quantity_per_source'])
  60. ? $productData['quantity_per_source']
  61. : [];
  62. // get sku by child id, because child sku can be changed if product with such sku already exists.
  63. $childProductId = $product->getExtensionAttributes()->getConfigurableProductLinks()[$key];
  64. $childProductSku = $this->getSkusByProductIdsInterface->execute([$childProductId])[$childProductId];
  65. $this->processSourceItems($quantityPerSource, $childProductSku);
  66. }
  67. }
  68. }
  69. }
  70. /**
  71. * @param array $sourceItems
  72. * @param string $productSku
  73. *
  74. * @return void
  75. */
  76. private function processSourceItems(array $sourceItems, string $productSku)
  77. {
  78. foreach ($sourceItems as $key => $sourceItem) {
  79. if (!isset($sourceItem[SourceItemInterface::STATUS])) {
  80. $sourceItems[$key][SourceItemInterface::QUANTITY] = $sourceItems[$key]['quantity_per_source'];
  81. $sourceItems[$key][SourceItemInterface::STATUS]
  82. = $sourceItems[$key][SourceItemInterface::QUANTITY] > 0 ? 1 : 0;
  83. }
  84. }
  85. $this->sourceItemsProcessor->process($productSku, $sourceItems);
  86. }
  87. }