Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessSourceItemConfigurationsObserver.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
20 
28 {
32  private $isSourceItemManagementAllowedForProductType;
33 
37  private $sourceItemsConfigurationProcessor;
38 
42  private $isSingleSourceMode;
43 
47  private $defaultSourceProvider;
48 
53  public function __construct(
54  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
55  SourceItemsConfigurationProcessor $sourceItemsConfigurationProcessor,
56  IsSingleSourceModeInterface $isSingleSourceMode,
57  DefaultSourceProviderInterface $defaultSourceProvider
58  ) {
59  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
60  $this->sourceItemsConfigurationProcessor = $sourceItemsConfigurationProcessor;
61  $this->isSingleSourceMode = $isSingleSourceMode;
62  $this->defaultSourceProvider = $defaultSourceProvider;
63  }
64 
69  public function execute(EventObserver $observer)
70  {
72  $product = $observer->getEvent()->getProduct();
73  if ($this->isSourceItemManagementAllowedForProductType->execute($product->getTypeId()) === false) {
74  return;
75  }
76 
78  $controller = $observer->getEvent()->getController();
79 
80  $assignedSources = [];
81  if ($this->isSingleSourceMode->execute()) {
82  $stockData = $controller->getRequest()->getParam('product', [])['stock_data'] ?? [];
83  $assignedSources[] = [
84  SourceItemInterface::SOURCE_CODE => $this->defaultSourceProvider->getCode(),
87  'notify_stock_qty_use_default' =>
89  ];
90  } else {
91  $sources = $controller->getRequest()->getParam('sources', []);
92  if (isset($sources['assigned_sources']) && is_array($sources['assigned_sources'])) {
93  $assignedSources = $sources['assigned_sources'];
94  }
95  }
96 
97  $this->sourceItemsConfigurationProcessor->process($product->getSku(), $assignedSources);
98  }
99 }
$controller
Definition: info.phtml:14
__construct(IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, SourceItemsConfigurationProcessor $sourceItemsConfigurationProcessor, IsSingleSourceModeInterface $isSingleSourceMode, DefaultSourceProviderInterface $defaultSourceProvider)
$stockData
Definition: products.php:27