Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Product model #17

Closed
naydav opened this issue May 19, 2017 · 0 comments
Closed

Update Product model #17

naydav opened this issue May 19, 2017 · 0 comments
Assignees

Comments

@naydav
Copy link

naydav commented May 19, 2017

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;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant