How to get stock name by website code in Magento 2?

This article is useful when your system has an inventory module enabled and uses multiple sources and stock functionality for a given website.

To Retrieve the Stock name by Website code, You need to use the interface Magento\InventorySalesApi\Api\StockResolverInterface.

Given Interface is responsible for getting the linked stock-related information for a certain sales channel.

You need to pass the website code to get a linked stock name for a system.

<?php
declare(strict_types=1);

namespace Rbj\Inventory\Model;

use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;

class GetStockNameForCurrentWebsite
{
    public function __construct(
        private StockResolverInterface $stockResolver
    ) {
    }

    public function getStockNameByWebsiteCode(): string
    {
        $websiteCode = 'base';

        $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
        return $stock->getName();
    }
}

You can get the stock name associated with the website code. Isn’t it simple to fetch stock names?