浏览文章
文章信息
Magento2 查询msi每个网站下产品的可售数量情况|get msi product saleable quantity
13652
示例:
<?php /** * @Author 秋枫雁飞 * @Email aiweline@qq.com/1714255949@qq.com * @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要 * 建议不要随意修改文件源码。 **/ namespace Aiweline\Core\Service\Magento\Catalog; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; class ProductService { private \Magento\InventorySalesApi\Api\GetProductSalableQtyInterface $getProductSalableQtyInterface; private \Magento\InventorySalesApi\Api\StockResolverInterface $stockResolverInterface; private \Magento\Store\Model\StoreManagerInterface $storeManager; function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\InventorySalesApi\Api\GetProductSalableQtyInterface $getProductSalableQtyInterface, \Magento\InventorySalesApi\Api\StockResolverInterface $stockResolverInterface ) { $this->getProductSalableQtyInterface = $getProductSalableQtyInterface; $this->stockResolverInterface = $stockResolverInterface; $this->storeManager = $storeManager; } /** * @Desc | 获得产品可售数 * @param \Magento\Catalog\Model\Product $product * @return float */ function getCurrentWebsiteStockSaleableQty(\Magento\Catalog\Model\Product $product) { try { return $this->getProductSalableQtyInterface->execute($product->getSku(), $this->getStockId()); } catch (InputException $e) { } catch (NoSuchEntityException $e) { } catch (LocalizedException $e) { } return 0; } /** * @Desc | 获得仓库ID * @return int|null * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ function getStockId() { return $this->stockResolverInterface->execute(SalesChannelInterface::TYPE_WEBSITE, $this->storeManager->getWebsite()->getCode())->getStockId(); } }