4

In my custom module, I'm using StockRegistryInterface to set qty and save my product. But on Magento 2.3 I started facing the issue of having 0 salable qty right after saving the product.

Default stock 0

After digging a bit, I saw that StockRegistryInterface is deprecated and it was replaced with Multi Source Inventory. How should I save the qty of my product now?

Here is the piece of code where I do it with StockRegistryInterface:

        /* @var ProductInterface $product */
        $stockItem = $this->stock->getStockItemBySku($product->getSku());
        $stockItem->setQty($this->estoque_disponivel);
        $stockItem->setIsInStock(true);
        $this->stock->updateStockItemBySku($product->getSku(), $stockItem);

3 Answers 3

6

Here you go!

/**
 * @var Magento\InventoryApi\Api\SourceItemsSaveInterface
 */
protected $sourceItemsSave;

/**
 * @var Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory
 */
protected $sourceItemFactory;

/**
 * @param SourceItemsSaveInterface $sourceItemsSave
 * @param SourceItemInterfaceFactory $sourceItemFactory
 */
public function __construct(
    Magento\InventoryApi\Api\SourceItemsSaveInterface $sourceItemsSave,
    Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory $sourceItemFactory,
) {
    $this->sourceItemsSave = $sourceItemsSave;
    $this->sourceItemFactory = $sourceItemFactory;
}

/**
 * @param $sku
 * @param $qty
 * @param $source
 */
public function setQtyToProduct($sku, $qty, $source)
{
    $sourceItem = $this->sourceItemFactory->create();
    $sourceItem->setSourceCode($source);
    $sourceItem->setSku($sku);
    $sourceItem->setQuantity($qty);
    $sourceItem->setStatus(1);

    $this->sourceItemsSave->execute([$sourceItem]);
}
2
  • It doesn't update the "Salable Qty", unfortunately
    – xpoback
    Commented Aug 25, 2022 at 13:14
  • It shouldn't update Saleable Qty, that logic is handled by Magento based on any items on orders.
    – Sondre
    Commented Dec 16, 2022 at 9:50
2

Use this page for the corresponding match for new Inventory API - https://github.com/magento-engcom/msi/wiki/Magento-MSI-APIs

1
  • 1
    Thanks a lot, I wish I had seen it before. I posted an answer showing how I did to save it in the default source.
    – Trimes
    Commented Apr 28, 2019 at 3:14
2

I found how to set qty to the product on the 'MSI way'.

PS: I'm using the default source.

The constructor:

private $sourceItemsSave;
private $sourceItemInterface;

public function __construct(
    SourceItemInterface $sourceItemInterface,
    SourceItemsSaveInterface $sourceItemsSave,
)
{
    $this->sourceItemsSave = $sourceItemsSave;
    $this->sourceItemInterface = $sourceItemInterface;
}

Saving the qty:

public function setQtyToProduct($product, $qty){

        /* @var ProductInterface $product */

        $this->sourceItemInterface->setSku($product->getSku());
        $this->sourceItemInterface->setQuantity($qty);
        $this->sourceItemInterface->setStatus(1);
        $this->sourceItemInterface->setSourceCode('default');
        $this->sourceItemsSave->execute([$this->sourceItemInterface]);
}
1
  • 3
    no need to request object SourceItemInterface $sourceItemInterface into the constructor, as what you need is to create and initialize this object on the fly. Thus, what you need to request is Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory and create the entity of SourceItemInterface with that factory when needed Commented Apr 29, 2019 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.