0

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?

0

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.

2
  • 1
    Thanks for the thorough response. The last point is very interesting and might just save us a ton of hassle! – BadNewsBrown Apr 8 at 8:20
  • I hope it does! Like I said, you'd want to experiment with it to make sure I've read the map correctly. Whether or not it does, please come back and leave an update if you think of it. Thanks! – kookaburra Apr 8 at 13:32

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.