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. Continue reading “How to get stock name by website code in Magento 2?”

How to fetch current stock id value from current website in Magento 2?

Magento with multiple stocks available at the website level and if you want to get the current stock ID value dynamically in Magento, you can fetch the value by using Magento inventory module functionality.

Retrieving stock ID will be helpful while you are using multi-source functionality in your website.

Let’s say your stock ID name is UK-stock (ID: 4) and you want to fetch the ID at any specific place in your code level, You can get it by using the GetStockIdForCurrentWebsite class. Continue reading “How to fetch current stock id value from current website in Magento 2?”

How to do Stock validation in Multi source inventory Magento 2?

Stock validation in Multi-source inventory is needed when you developing functionality with MSI of Magento 2.

Stock Validation in MSI using native interface class.
Magento\InventoryApi\Model\StockValidatorInterface

StockValidatorInterface has method called validate(StockInterface $stock);

If you want to validate the Stock of MSI, You just need to implement StockValidatorInterface in your class.

You can validate Stock related information for your requirements.

A simple example for a Stock name is valid or not using StockValidatorInterface.

<?php
namespace Jesadiya\Inventory\Model\Stock\Validator;

use Magento\Framework\Validation\ValidationResult;
use Magento\Framework\Validation\ValidationResultFactory;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventoryApi\Model\StockValidatorInterface;

/**
 * Check that name is valid
 */
class NameValidator implements StockValidatorInterface
{
    /**
     * @var ValidationResultFactory
     */
    private $validationResultFactory;

    /**
     * @param ValidationResultFactory $validationResultFactory
     */
    public function __construct(ValidationResultFactory $validationResultFactory)
    {
        $this->validationResultFactory = $validationResultFactory;
    }

    /**
     * @param StockInterface $stock
     * @return ValidationResult
     */
    public function validate(StockInterface $stock): ValidationResult
    {
        $value = (string)$stock->getName();

        if ('' === trim($value)) {
            $errors[] = __('"%field" can not be empty.', ['field' => StockInterface::NAME]);
        } else if ('Default Stock' === trim($value)) {
            $errors[] = __('"%field" is not allowed.', ['field' => StockInterface::NAME]);
        } else {
            $errors = [];
        }
        return $this->validationResultFactory->create(['errors' => $errors]);
    }
}

Using the above code, You can validate your Stock with custom validation. In the validate method, You need to pass StockInterface Object as a parameter.

In the above code snippet, Validate the Stock is not empty and the Stock name does not equal to Default Stock.

When you Developing any functionality, You can set your custom validation for Stock data