Magento – Magento 2 Rest Api get Products List including qty field

magento-2.1productsqtyrest api

I am trying to make an api call to get a list of products. I am using searchCriteria to filter the products collection. The following information of the product is needed:

sku, name, id, price, status, visibility, type_id, qty, image-url, small-image-url

The following call gives all information, excluding qty:

http://127.0.0.1/rest/V1/products/?searchCriteria[filter_groups][0][filters][0][field]=price&searchCriteria[filter_groups][0][filters][0][value]=25&searchCriteria[filter_groups][0][filters][0][condition_type]=from&searchCriteria[filter_groups][0][filters][1][field]=price&searchCriteria[filter_groups][0][filters][1][value]=500&searchCriteria[filter_groups][0][filters][1][condition_type]=to&searchCriteria[pageSize]=2&searchCriteria[currentPage]=1

{
"items": [
    {
        "id": 1,
        "sku": "Test",
        "name": "Test 100x100",
        "attribute_set_id": 4,
        "price": 200,
        "status": 1,
        "visibility": 4,
        "type_id": "simple",
        "created_at": "2017-08-23 17:48:05",
        "updated_at": "2017-08-23 17:48:05",
        "weight": 1,
        "extension_attributes": [],
        "product_links": [],
        "tier_prices": [],
        "custom_attributes": [
            {
                "attribute_code": "meta_title",
                "value": "Test"
            },
            {
                "attribute_code": "meta_keyword",
                "value": "Test"
            },
            {
                "attribute_code": "meta_description",
                "value": "Test "
            },
            {
                "attribute_code": "image",
                "value": "/c/a/cat1.png"
            },
            {
                "attribute_code": "small_image",
                "value": "/c/a/cat1.png"
            },
            {
                "attribute_code": "thumbnail",
                "value": "/c/a/cat1.png"
            },
            {
                "attribute_code": "options_container",
                "value": "container2"
            },
            {
                "attribute_code": "required_options",
                "value": "0"
            },
            {
                "attribute_code": "has_options",
                "value": "0"
            },
            {
                "attribute_code": "url_key",
                "value": "test"
            },
            {
                "attribute_code": "swatch_image",
                "value": "/c/a/cat1.png"
            },
            {
                "attribute_code": "tax_class_id",
                "value": "2"
            },
            {
                "attribute_code": "gift_message_available",
                "value": "2"
            },
            {
                "attribute_code": "laenge_intern",
                "value": "100.0000"
            },
            {
                "attribute_code": "breite_intern",
                "value": "100.0000"
            },
            {
                "attribute_code": "sw_featured",
                "value": "0"
            }
        ]
    },
    {
        "id": 2,
        "sku": "Test-1",
        "name": "Test 100x110",
        "attribute_set_id": 4,
        "price": 200,
        "status": 1,
        "visibility": 4,
        "type_id": "simple",
        "created_at": "2017-08-29 20:20:54",
        "updated_at": "2017-08-29 20:20:54",
        "weight": 1,
        "extension_attributes": [],
        "product_links": [],
        "tier_prices": [],
        "custom_attributes": [
            {
                "attribute_code": "meta_title",
                "value": "Test"
            },
            {
                "attribute_code": "meta_keyword",
                "value": "Test"
            },
            {
                "attribute_code": "meta_description",
                "value": "Test "
            },
            {
                "attribute_code": "image",
                "value": "/c/a/cat1_1.png"
            },
            {
                "attribute_code": "small_image",
                "value": "/c/a/cat1_1.png"
            },
            {
                "attribute_code": "thumbnail",
                "value": "/c/a/cat1_1.png"
            },
            {
                "attribute_code": "options_container",
                "value": "container2"
            },
            {
                "attribute_code": "required_options",
                "value": "0"
            },
            {
                "attribute_code": "has_options",
                "value": "0"
            },
            {
                "attribute_code": "url_key",
                "value": "test-1"
            },
            {
                "attribute_code": "swatch_image",
                "value": "/c/a/cat1_1.png"
            },
            {
                "attribute_code": "tax_class_id",
                "value": "2"
            },
            {
                "attribute_code": "gift_message_available",
                "value": "2"
            },
            {
                "attribute_code": "laenge_intern",
                "value": "100.0000"
            },
            {
                "attribute_code": "breite_intern",
                "value": "110.0000"
            },
            {
                "attribute_code": "sw_featured",
                "value": "0"
            }
        ]
    }
],
"search_criteria": {
    "filter_groups": [
        {
            "filters": [
                {
                    "field": "price",
                    "value": "25",
                    "condition_type": "from"
                },
                {
                    "field": "price",
                    "value": "500",
                    "condition_type": "to"
                }
            ]
        }
    ],
    "page_size": 2,
    "current_page": 1
},
"total_count": 6
}

In the Doc Site http://devdocs.magento.com/guides/v2.1/howdoi/webapi/filter-response.html they mention the 'fields' parameter to get the items qty. I tried the following call, but it still does not include qty into the response:

http://127.0.0.1/rest/V1/products/?searchCriteria[filter_groups][0][filters][0][field]=price&searchCriteria[filter_groups][0][filters][0][value]=25&searchCriteria[filter_groups][0][filters][0][condition_type]=from&searchCriteria[filter_groups][0][filters][1][field]=price&searchCriteria[filter_groups][0][filters][1][value]=500&searchCriteria[filter_groups][0][filters][1][condition_type]=to&searchCriteria[pageSize]=2&searchCriteria[currentPage]=1&fields=items[id,sku,name,qty]

As expected it returns items with id, sku, name. But no qty.

How to get item qty in the response ?

Thanks

Best Answer

In my response I found qty in extension_attributes.

"extension_attributes": {
    "stock_item": {
        "item_id": 4876,
        "product_id": 4884,
        "stock_id": 1,
        "qty": 0,
        ...

If you use custom product type you need to add special config for product type (etc/product_types.xml).

isQty="true"

Example:

<type name="mySpecialType" label="MySpecialType Product" isQty="true" ...
Related Topic