4

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.

1
  • are you able to share the module - would be helpful to know how this is done :) Many thanks
    – user41939
    Commented Jul 8, 2016 at 13:50

2 Answers 2

3

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.

4
  • Hi KAndy, thanks for the response but I meant loading and saving inventory in batch - not retrieving. I'll update my question so it's clearer.
    – BAF
    Commented Jun 5, 2016 at 23:13
  • Thanks KAndy, this was helpful - I will need to adapt this to update by SKU but a good start - thanks for revising answer.
    – BAF
    Commented Jun 7, 2016 at 7:38
  • @KAndy so where does the StockStatusRepository belong? And how would you call this custom endpoint? This is for modifying the stock levels so why would it be using method="GET" - should be using PUT surely?
    – ajmedway
    Commented Mar 29, 2017 at 14:25
  • Guys, is there appeared any module for this purpose?
    – Kirby
    Commented Aug 29, 2018 at 14:12
1

In the end i created my own stock object and declared it in the phpdoc for the API method as @param MyCo\MyClass\Api\Data\StockItems[] $stockItems.

which translates to

{stockItems:[{product_id:id, quantity:0},.. etc ..]}

I then iterated through the StockItems array, adding inventory with an injected Magento stockitem object.

It is working now at least.

2
  • 1
    can you please share the code what you have achieved Commented Feb 28, 2017 at 7:30
  • It would be nice to have the module! Thx in advance.
    – Kirby
    Commented Aug 29, 2018 at 14:14

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.