2

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
}
}
  • as far as I see you should pass an array of sourceItems (i.e. objects of type \Magento\InventoryApi\Api\Data\SourceItemInterface) to the execute method. What is your code which gives that error? – HelgeB Feb 21 at 13:19
  • @HelgeB see my edit in the question. – MartinE Feb 21 at 13:47
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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