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

Check Is Product assigned to Stock or not using Multi-store inventory (MSI) Magento 2.

When you have set up a Multi-store inventory for your eCommerce projects in Magento 2 with multiple stocks is set up for sending an item shipment from different locations or warehouse.

You can verify Is product assigned to specific Stock or not using this article. Continue reading “Check Is Product assigned to Stock or not using Multi-store inventory (MSI) Magento 2.”

How to get Payment Gateway token from the Vault Payment order Magento 2?

Get Payment Gateway token from the Order in Magento 2 will be useful in the future, if any payment related information needed for that specific transaction.

Vault payment method stores the payment’s gateway token for the order with saving credit card information for the future.

The customer has placed an order from the site with checked Save Credit Card details for the next transaction from the Payment step, Vault store the gateway token for the Payment. Continue reading “How to get Payment Gateway token from the Vault Payment order Magento 2?”