3

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

0

2 Answers 2

1

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" ...
0

The stock_item (including qty) is no longer included in a extension_attributes. It was deprecated a while ago (see issue #24418) though the REST documentation on Adobe's site still shows it included n the response from the /V1/products endpoint.

You can use this plugin to bring it back: https://github.com/menacoders/Stock-Info-API-searchCriteria

Or a simple (no code) plugin which adds a single "qty" field to the extension_attributes: https://github.com/cristoper/mage_qtyext

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.