Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
SaveInventoryDataObserver Class Reference
Inheritance diagram for SaveInventoryDataObserver:
ObserverInterface

Public Member Functions

 __construct (StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry, StockItemValidator $stockItemValidator=null)
 
 execute (EventObserver $observer)
 
- Public Member Functions inherited from ObserverInterface
 execute (Observer $observer)
 

Detailed Description

Saves stock data from a product to the Stock Item

Deprecated:
100.2.0 Stock data should be processed using the module API
See also
StockItemInterface when you want to change the stock data
StockStatusInterface when you want to read the stock data for representation layer (storefront)
StockItemRepositoryInterface::save as extension point for customization of saving process

Definition at line 25 of file SaveInventoryDataObserver.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( StockConfigurationInterface  $stockConfiguration,
StockRegistryInterface  $stockRegistry,
StockItemValidator  $stockItemValidator = null 
)
Parameters
StockConfigurationInterface$stockConfiguration
StockRegistryInterface$stockRegistry
StockItemValidator$stockItemValidator

Definition at line 81 of file SaveInventoryDataObserver.php.

85  {
86  $this->stockConfiguration = $stockConfiguration;
87  $this->stockRegistry = $stockRegistry;
88  $this->stockItemValidator = $stockItemValidator ?: ObjectManager::getInstance()->get(StockItemValidator::class);
89  }

Member Function Documentation

◆ execute()

execute ( EventObserver  $observer)

Saving product inventory data

Takes data from the stock_data property of a product and sets it to Stock Item. Validates and saves Stock Item object.

Parameters
EventObserver$observer
Returns
void

Definition at line 100 of file SaveInventoryDataObserver.php.

101  {
102  $product = $observer->getEvent()->getProduct();
103  $stockItem = $this->getStockItemToBeUpdated($product);
104 
105  if ($product->getStockData() !== null) {
106  $stockData = $this->getStockData($product);
107  $stockItem->addData($stockData);
108  }
109  $this->stockItemValidator->validate($product, $stockItem);
110  $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
111  }
$stockData
Definition: products.php:27

The documentation for this class was generated from the following file: