-
Notifications
You must be signed in to change notification settings - Fork 254
Closed
Description
Full context in documentation
[HLD] Inconsistent saving of Stock Data
Description
Сreate a single point of data (source of truth) for write operations.
Proxy all methods with stock data manipulation to StockItem.
Prototype:
// \Magento\Catalog\Model\Product
class Product
...
public function __construct(
...
array $data = [],
StockRegistryInterface $stockRegistry = null,
HydratorInterface $hydrator = null
) {
...
$this->stockRegistry = $stockRegistry ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(StockRegistryInterface::class);
$this->hydrator = $hydrator ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(HydratorInterface::class);
}
...
/**
* {@inheritdoc}
*
* This method has been overridden only for backward compatible work with stock item (stock_data,
* quantity_and_stock_status keys)
*
* Use \Magento\Catalog\Api\Data\ProductExtensionInterface::getStockItem for retrieving of stock item data (or stock
* status index)
* Use \Magento\Catalog\Api\Data\ProductInterface for retrieving of product data
*
*/
public function getData($key = '', $index = null)
{
if ('stock_data' === $key) {
$result = $this->getStockData();
} elseif ('quantity_and_stock_status' === $key) {
$result = $this->getQuantityAndStockStatus();
} else {
$result = parent::getData($key, $index);
}
return $result;
}
/**
* {@inheritdoc}
*
* This method has been overridden only for backward compatible work with stock item (stock_data,
* quantity_and_stock_status keys)
*
* Use \Magento\Catalog\Api\Data\ProductExtensionInterface::setStockItem for updating of stock item data
* Use \Magento\Catalog\Api\Data\ProductInterface for updating of product data
*
*/
public function setData($key, $value = null)
{
if ('stock_data' === $key) {
$result = $this->setStockData($value);
} elseif ('quantity_and_stock_status' === $key && is_array($value)) {
$result = $this->setQuantityAndStockStatus($value);
} else {
$result = parent::setData($key, $value);
}
return $result;
}
/**
* @return array
* @deprecated Use \Magento\Catalog\Api\Data\ProductExtensionInterface::getStockItem for retrieving of stock item
* data (or stock status index)
*/
public function getStockData()
{
$stockItem = $this->resolveStockItem();
return $this->hydrator->extract($stockItem);
}
/**
* @param array $stockData
* @return $this
* @deprecated Use \Magento\Catalog\Api\Data\ProductExtensionInterface::setStockItem for updating of stock item data
*/
public function setStockData(array $stockData)
{
$stockItem = $this->resolveStockItem();
$this->dataObjectHelper->populateWithArray(
$stockItem,
$stockData,
StockItemInterface::class
);
return $this;
}
/**
* @return array
* @deprecated Use \Magento\Catalog\Api\Data\ProductExtensionInterface::getStockItem for retrieving of stock item
* data (or stock status index)
*/
public function getQuantityAndStockStatus()
{
return $this->getStockData();
}
/**
* @param array $quantityAndStockStatusData
* @return $this
* @deprecated Use \Magento\Catalog\Api\Data\ProductExtensionInterface::setStockItem for updating of stock item data
*/
public function setQuantityAndStockStatus(array $quantityAndStockStatusData)
{
return $this->setStockData($quantityAndStockStatusData);
}
/**
* @return StockItemInterface
*/
private function resolveStockItem()
{
$extensionAttributes = $this->getExtensionAttributes();
$stockItem = $extensionAttributes->getStockItem();
if (null === $stockItem) {
$stockItem = $this->stockRegistry->getStockItem($this->getId());
$extensionAttributes->setStockItem($stockItem);
$stockItem->setProduct($this);
}
return $stockItem;
}Metadata
Metadata
Assignees
Labels
No labels