How to Get Stock ID of Particular Website in Magento 2
Magento 2 multi-stock inventory makes the control of inventory for single or multiple sources and sales channels possible!
Equipping Magento 2 store with warehouse management as well as inventory management is one of the most crucial tasks for store owners. If you have more than one warehouse under your control, it is necessary to control all data in the inventory and get that data whenever you need it.
In order to work with inventory among multiple warehouses easily, generate inventory-related reports, and manage stock effectively, one often needs to get stock id of particular website in Magento 2.
Check out the below method to do so.
Method to Get Stock ID of Particular Website in Magento 2
Use the below code in your helper file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace Vendor\Module\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite; /** * Class GetStockIdForWebsite */ class GetStockIdForWebsite extends AbstractHelper { /** * @var GetAssignedStockIdForWebsite */ private $getAssignedStockIdForWebsite; /** * GetStockIdForWebsite constructor. * @param Context $context * @param GetAssignedStockIdForWebsite $getAssignedStockIdForWebsite */ public function __construct(Context $context, GetAssignedStockIdForWebsite $getAssignedStockIdForWebsite) { parent::__construct($context); $this->getAssignedStockIdForWebsite = $getAssignedStockIdForWebsite; } /** * @param $websiteCode * @return int|null */ public function getStockIdForWebsite($websiteCode) { return $this->getAssignedStockIdForWebsite->execute($websiteCode); } } |
That’s it.
Any doubts? Do mention them in the Comments section below.
I would be glad to help you out.
Also, please share the solution with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Set Up Staging Environment in Magento 2
How to Redirect Customer to Custom Page after Login in Magento 2
Next