Question

We currently run Magento without MSI. The ERP sends stock updates via extension attributes on the (bulk) product endpoint. This enables us to do most product updates in on API call. Very convenient.

We are looking to implement MSI, but it seems it is not possible to set multiple source quantities as product extension attributes and we would need to start making two API calls (maybe more) to create a product and assign some stock....

We would like to continue to do a single API call to create/update product AND set the stock in MSI like so:

{ 
"product" : {
    "sku": "24-MB01-3",
    "name": "My Duffle Bagz 3",
    "attribute_set_id": 15,
    "price": 34,
    "status": 1,
    "visibility": 4,
    "type_id": "simple",
    "extension_attributes": {
        "category_links": [
            {
                "position": 0,
                "category_id": "3"
            },
            {
                "position": 0,
                "category_id": "4"
            }
        ],
        {
        "sourceItems": [
         {
               "sku": "24-MB01-3",
               "source_code": "NW",
               "quantity": 1000,
               "status": 1
         },
         {
             "sku": "24-MB01-3",
             "source_code": "LN",
             "quantity": 500,
             "status": 1
         }]
       }
    }
}
}

Because we use bulk endpoints, our current process is asynchronous, so knowing when a particular product is created and we can then set the stock is difficult.

Is there any way to do this rather than having to use the separate source-items endpoint?

Was it helpful?

Solution

First, the bad news. No, there's no way in the existing async/bulk/V1/product endpoint to do this. All this does is iterate over each of the products you give it, and queue up one message per product which will then be consumed one at a time by \Magento\Catalog\Model\ProductRepository::save(). You can see in this function that it doesn't do MSI updates.

Now, the better news. If you really want to do this and you can control what your ERP passes to Magento, then Magento makes it relatively easy to set up your own API endpoints. Sanjay's tutorial is a good starting point, although you'll likely need to supplement this foundation with information on integrations/authentication and resources.

If you go this route, you could set up your own endpoint that receives a body you can split into (1) an object of products that you then pass to async/bulk/V1/products and (2) an object of source items that you then pass to V1/inventory/source-items. So while under the hood you're still technically hitting two endpoints, your ERP is only calling to Magento once.

Finally, the (possibly) best news. You wrote:

Because we use bulk endpoints, our current process is asynchronous, so knowing when a particular product is created and we can then set the stock is difficult.

I take it that this business rule is a significant driver of your search for a single endpoint.

The endpoint V1/inventory/source-items receives an array of source items at \Magento\Inventory\Model\SourceItem\Command\Handler\SourceItemsSaveHandler::execute(), which inserts all of them into the inventory_source_item table with a single SQL query. This table doesn't appear to care whether or not a SKU exists. As near as I can tell, Magento doesn't attempt to validate whether or not a SKU exists before insertion into this table.

So technically, when you hit the source-items endpoint, Magento may not care if you haven't created those SKUs yet. If that's the case (and you'd want to experiment with it), then you could hit both the products and source-items endpoints on your own schedule without worrying about which operation comes first. This may alleviate one of your main concerns and eliminate the need for one endpoint to rule them all.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top