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:
{
"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:
As expected it returns items with id, sku, name. But no qty.
How to get item qty in the response ?
Thanks
2 Answers 2
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" ...
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
-
see also: catswhisker.xyz/log/2021/8/22/magento_suckscristoper– cristoper2021年10月05日 18:05:31 +00:00Commented Oct 5, 2021 at 18:05