Magento 2 REST API – Batch Update Stock Items

apicustommagento2rest

I have built a custom module that will save a single stock item via REST API (similar to what is already available) – however, how would this be done with multiple stock items within the same request (like a batch update). I've tried to make the @param an /../../inventoryItem[] in my method definition in the module/api folder, but it doesn't seem to work.

I see we can't simply send an array as we could in Magento 1.

the structure would be something like

{"inventory":[{"sku"=>"some_sku", "qty"=>10}, {"sku"=>"some_sku2", "qty"=>12}]}.

If someone could lead me over this architecturally – especially with the interfaces/classes required, then that would be helpful.

I assume I'd need such classes (as below) – one for the inventory object, another for the array – but I haven't worked extensively with interfaces and having trouble defining things in a Magento way.

<?php

class InventoryObject {

    private $sku;
    private $qty;

    public function __construct($sku, $qty) {
        $this->qty = $qty;
        $this->sku = $sku;
    }

    public function getQty(){
        return $this->qty;
    }

    public function setQty($qty){
        $this->qty = $qty;
    }

    public function getSku(){
        return $this->sku;
    }

    public function setSku($sku){
        $this->sku = $sku;
    }

}

class InventoryObjectArray {

    private $inventoryObject = array();

    public function __construct(InventoryObject $io) {
        $this->inventoryObject[] = $io;     
    }


    public function setInventoryObject(InventoryObject $io) {
        $this->inventoryObject[] = $io; 
    }

    public function getInventoryObject() {

        return $this->inventoryObject;      
    }

}

then after this, I can loop through and save stock items one by one. Do I need to define this?
Or just use the standard stockitem object and pass into an array?
In any case, bit confused and need assistance structuring things properly.

Best Answer

Looks like all already ready for you in Magento.

You need only one file in your module etc/webapi.xml with content

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/stockItems" method="GET">
        <service class="\Magento\CatalogInventory\Api\StockStatusRepositoryInterface" method="getList"/>
        <resources>
            <resource ref="Magento_CatalogInventory::cataloginventory"/>
        </resources>
    </route>
</routes>

<?php
class StockStatusRepository implements \Magento\CatalogInventory\Api\StockStatusRepositoryInterface
{
    ....

    /**
     * @param  \Magento\CatalogInventory\Api\Data\StockItemInterface[] $stockItems
     * @return bool
     */ 
    public function batchUpdate($stockItems)
    {
        foreach ($stockItems as $items) {
            $this->stockItemRepo->save($stockItems);
        }
        return true; 
     }
}

And all your work will be done.