Magento 2.3 ERP integration and update stock via Bulk Api
Almost every merchant wants to update or synchronize product stock in Magento over API. In this post we will explain how to do it on Magento 2.3 version. Probably you know that Magento 2.3 is coming with built in support for RabbitMQ.
Support for RabbitMQ is excellent thing because with RabbitMQ we can use asynchronous API and process big amount of API requests.
What does it mean? It means that you can send few thousand of requests from ERP to Magento and you don’t need to wait for finishing it. All ERP requests will be stored in RabbitMQ queue and executed / processed in background by RabbitMQ consumer.
RabbitMQ consumer is different Magento script which is in charge for processing messages from queue.
In first example we will demonstrate how to update product stock over standard Magento REST API.
If you open webapi.xml (config file for Magento API) file inside module-catalog/inventory module you will see next declaration:
<route url="/V1/products/:productSku/stockItems/:itemId" method="PUT">
<service class="Magento\CatalogInventory\Api\StockRegistryInterface" method="updateStockItemBySku"/>
As you can see this is declaration for route / update stock item. For testing purpose I’m using great tool Postman for creating api requests to instance of Magento 2.3. You can see in screen shot how to configure Postman for updating process.
Api endpoin url is: http://m23.lo/rest/V1/products/24-MB01/stockItems/1 .
Our call will update qty for product SKU: 24-MB01 and stock_item: 1.
Next example will demonstrate how to update stock over Magento async or bulk api.
All logic for async api is inside module: module-webapi-async. All api requests to asny api should contains async part of url. Take a look on our API endpoint url: http://m23.lo/rest/async/V1/products/24-MB01/stockItems/1 . I hope that you noticed what is different in url. Every request to asny api should start with ‘rest/async/‘.
Below is screen shoot from Postman:
If you send request to async api you will get next response from Magento:
Your request is accepted and stored in RabbitMQ queu but not yet processed. For processing request there is specific script in Magento which is called RabbitMQ consumer. How to start Rabbit consumer we will explain in some of the further posts.
If you need any explanation feel free to ask Magento Certified Developer.