/** * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject * @param callable $proceed * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param bool $saveOptions * @return \Magento\Catalog\Api\Data\ProductInterface * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function aroundSave(\Magento\Catalog\Api\ProductRepositoryInterface $subject, \Closure $proceed, \Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { /** @var \Magento\Catalog\Api\Data\ProductInterface $result */ $result = $proceed($product, $saveOptions); /* @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */ $stockItem = $this->getStockItemToBeUpdated($product); if ($stockItem == null) { return $result; } // set fields that the customer should not care about $stockItem->setProductId($result->getId()); $stockItem->setWebsiteId($this->storeManager->getStore($result->getStoreId())->getWebsiteId()); $this->stockRegistry->updateStockItemBySku($result->getSku(), $stockItem); // since we just saved a portion of the product, force a reload of it before returning it return $subject->get($result->getSku(), false, $result->getStoreId(), true); }
public function testUpdateStockItemBySku() { $itemId = 1; $this->stockItem->expects($this->once())->method('setProductId')->willReturnSelf(); $this->stockItem->expects($this->once())->method('getData')->willReturn([]); $this->stockItem->expects($this->once())->method('addData')->willReturnSelf(); $this->stockItem->expects($this->atLeastOnce())->method('getItemId')->willReturn($itemId); $this->assertEquals($itemId, $this->stockRegistry->updateStockItemBySku($this->productSku, $this->stockItem)); }