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.

| improve this question | | | | |
  • are you able to share the module - would be helpful to know how this is done :) Many thanks – user41939 Jul 8 '16 at 13:50
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.

| improve this answer | | | | |
  • 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 Jun 5 '16 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 Jun 7 '16 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 Mar 29 '17 at 14:25
  • Guys, is there appeared any module for this purpose? – Kirby Aug 29 '18 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.

| improve this answer | | | | |
  • 1
    can you please share the code what you have achieved – prasad maganti Feb 28 '17 at 7:30
  • It would be nice to have the module! Thx in advance. – Kirby Aug 29 '18 at 14:14

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.