Magento 2.3 – Update Source Stock

magento2.3magento2.3.0

Im currently creating a module that should update product quantity per source, based on SKU. Im using Magento\InventoryApi\Api\SourceItemsSaveInterface and Magento\InventoryApi\Api\Data\SourceItemsInterface.

Im trying to use the execute() function from SourceItemsSaveInterface but it expects parameter: \Magento\InventoryApi\Api\Data\SourceItemInterface[] $sourceItems.

What's unclear to me, is how to get that parameter from the SourceItemInterface and what kind of data it should be.

Currently it gives me the below error:

Fatal error: Uncaught TypeError: Argument 1 passed to Magento\InventoryApi\Model\SourceItemValidatorChain::validate() must implement interface Magento\InventoryApi\Api\Data\SourceItemInterface, array given....

Using below code:

<?php
namespace <vendor>/<module>;

use Magento\InventoryApi\Api;
use Magento\InventoryApi\Api\Data;

class Temp
{
protected $_sourceItemsSaveInterface;

protected $_sourceItemInterface;

public function __construct(
    Api\SourceItemsSaveInterface $sourceItemsSaveInterface,
    Data\SourceItemInterface $sourceItemInterface
)
{
    $this->_sourceItemsSaveInterface = $sourceItemsSaveInterface;
    $this->_sourceItemInterface = $sourceItemInterface;
}

public function testFunction() {
    $sourceItemInterface = $this->_sourceItemInterface;

    $sourceItemInterface->setSourceCode('source');
    $sourceItemInterface->setSku('12345678');
    $sourceItemInterface->setQuantity(50);
    $sourceItemInterface->setStatus(1);

    $array = array(
        'sourceItems' => array('source_code' => 'source', 'sku' => '12345678', 'quantity'=>50, 'status'=>1)
    );

    $this->_sourceItemsSaveInterface->execute($array);

    //I need to know what kind of parameter/data should be set in execute() function
}
}

Best Answer

You need to create sourceItem objects, therefore I have injected a factory for that in your constructor. You can use the following code - it should work for you. since the constructor has changes, you might need to remove the corresponding generated file for your class

<?php
namespace <vendor>/<module>;

use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;

class Temp
{
   protected $_sourceItemsSaveInterface;

   protected $_sourceItemFactory;

   public function __construct(
       SourceItemsSaveInterface $sourceItemsSaveInterface,
       SourceItemInterfaceFactory $sourceItemFactory
   )
   {
       $this->_sourceItemsSaveInterface = $sourceItemsSaveInterface;
       $this->_sourceItemFactory = $sourceItemFactory;
   }

   public function testFunction() {

       //create the sourceItem using the factory
       $sourceItem = $this->_sourceItemFactory->create();
       $sourceItem->setSourceCode('source');
       $sourceItem->setSku('12345678');
       $sourceItem->setQuantity(50);
       $sourceItem->setStatus(1);

       //pass the sourceItem as an array element, you can add more source items in the same call as further array elements
       $this->_sourceItemsSaveInterface->execute([$sourceItem]);
   }
}

I hope that helps.

Related Topic