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
I hope that helps.