浏览文章
文章信息
Magento2 以编程方式为产品设置库存|set stock quantity for product programmatically
15807
1、代码
public function __construct( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry ) { $this->stockRegistry = $stockRegistry; parent::__construct(); } public function yourMethod() { $sku = 'ABC123'; $qty = 10; $stockItem = $this->stockRegistry->getStockItemBySku($sku); $stockItem->setQty($qty);// 设置库存数量 $stockItem->setIsInStock((bool)$qty); // 启用库存 $this->stockRegistry->updateStockItemBySku($sku, $stockItem); }