In some cases you have to load stock item using product_id in Magento2. Today’s snippet we are showing how to do that.
For illustration purpose we use a model example.
namespace Vendor\Module\Model; use Magento\CatalogInventory\Model\Stock\StockItemRepository; class HelloWorld extends extends \Magento\Framework\Model\AbstractModel { protected $stockItemRepository; public function __construct( StockItemRepository $stockItemRepository ) { $this->stockItemRepository = $stockItemRepository; } public function getStockItem($productId) { return $this->stockItemRepository->get($productId); } }
Leave a Reply