Openresty : Programming with Magento/Mysql – Get Inventory of Products REST API

Skills Requried – LUA, nginx, mysql

If you have nginx-openresty installed, open the vhost file of your domain, than follow this:

Let your REST API path is http://<your-domain>.com/getstockserv?p=123450,12145

where p is the parameter where all product ids of Magento Products has been supplied.

Now add this entry to the vhost file –

location ^~ /getstockserv {
content_by_lua ‘
local mysql = require “resty/mysql”;
local cjson = require “cjson”;
args = ngx.req.get_uri_args();
local query = “select product_id from cataloginventory_stock_item where is_in_stock = 0 and product_id in (“.. args.p ..”)”;
local db, err = mysql:new();

local ok, err, errno, sqlstate = db:connect{
host = “<Enter Mysql Hostname>”,
port = 3306,
database = “<Enter Database Name>”,
user = “<Enter Mysql User>”,
password = “<Enter Mysql Password>”,
max_packet_size = 1024 * 1024 };
res, err, errno, sqlstate = db:query(query);
ngx.header[“Content-Type”] = “application/json”;

Reload nginx : nginx -s reload

Voila!! Now you can get the inventory of products without using Magento.