Create Products using Rest API : Magento V2.0 - magento

I have been trying to create a product using Rest API for Magento Version 2.0.
I am using Postman to test the rest api.
URL : http://13.91../rest/V1/products
I have added the following headers to the request.
Authorization : Bearer **********************
Content-Type : application/json
JSON BODY
{
"sku":"10090-White-XL",
"store_view_code":"",
"attribute_set_code":"ColorSize",
"product_type":"virtual",
"categories":"Menswear/Tops",
"product_websites":"base",
"name":"10090-White-XL",
"description":"<p>Precise Long-Sleeve Shirt in Black, Denim, or White.</p>",
"short_description":"",
"weight":"",
"product_online":1,
"tax_class_name":"Taxable Goods",
"visibility":"Not Visible Individually",
"price":119,
"special_price":"",
"special_price_from_date":"",
"special_price_to_date":"",
"url_key":"10090-white-xl",
"meta_title":"Precise Long-Sleeve Shirt",
"meta_keywords":"Precise Long-Sleeve Shirt",
"meta_description":"Precise Long-Sleeve Shirt <p>Precise Long-Sleeve Shirt in Black, Denim, or White.</p>",
"base_image":"",
"base_image_label":"",
"small_image":"",
"small_image_label":"",
"thumbnail_image":"",
"thumbnail_image_label":"",
"swatch_image":"",
"swatch_image_label":"",
"created_at":"3/23/16, 2:15 PM",
"updated_at":"3/23/16, 2:15 PM",
"new_from_date":"",
"new_to_date":"",
"display_product_options_in":"Block after Info Column",
"map_price":"",
"msrp_price":"",
"map_enabled":"",
"gift_message_available":"",
"custom_design":"",
"custom_design_from":"",
"custom_design_to":"",
"custom_layout_update":"",
"page_layout":"",
"product_options_container":"",
"msrp_display_actual_price_type":"",
"country_of_manufacture":"",
"additional_attributes":"color=White,size=XL",
"qty":null,
"out_of_stock_qty":0,
"use_config_min_qty":1,
"is_qty_decimal":0,
"allow_backorders":0,
"use_config_backorders":1,
"min_cart_qty":1,
"use_config_min_sale_qty":1,
"max_cart_qty":10000,
"use_config_max_sale_qty":1,
"is_in_stock":0,
"notify_on_stock_below":1,
"use_config_notify_stock_qty":1,
"manage_stock":0,
"use_config_manage_stock":0,
"use_config_qty_increments":1,
"qty_increments":0,
"use_config_enable_qty_inc":0,
"enable_qty_increments":0,
"is_decimal_divided":0,
"website_id":1,
"related_skus":"",
"crosssell_skus":"",
"upsell_skus":"",
"additional_images":"",
"additional_image_labels":"",
"hide_from_product_page":"",
"bundle_price_type":"",
"bundle_sku_type":"",
"bundle_price_view":"",
"bundle_weight_type":"",
"bundle_values":"",
"configurable_variations":"",
"configurable_variation_labels":"",
"associated_skus":""
}
The error that I get is {"message":"%fieldName is a required field.","parameters":{"fieldName":"product"}}
It will be great if someone could let me know how I could add a product. I have checked all the documents and but could not find an answer.

I have found the answer to my question. The json structure need to be in this format:
{
"product":{
"id": 12345,
"sku": "10090-White-XL",
"name": "10090-White-XL",
"attribute_set_id": 9,
"price": 119,
"status": 1,
"visibility": 1,
"type_id": "virtual",
"created_at": "2016-04-05 23:04:09",
"updated_at": "2016-04-05 23:04:09",
"product_links": [],
"options": [],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "description",
"value": "<p>Precise Long-Sleeve Shirt in Black, Denim, or White.</p>"
},
{
"attribute_code": "meta_title",
"value": "Precise Long-Sleeve Shirt"
},
{
"attribute_code": "meta_keyword",
"value": "Precise Long-Sleeve Shirt"
},
{
"attribute_code": "meta_description",
"value": "Precise Long-Sleeve Shirt <p>Precise Long-Sleeve Shirt in Black, Denim, or White.</p>"
},
{
"attribute_code": "color",
"value": "11"
},
{
"attribute_code": "options_container",
"value": "container2"
},
{
"attribute_code": "required_options",
"value": "0"
},
{
"attribute_code": "has_options",
"value": "0"
},
{
"attribute_code": "url_key",
"value": "10090-white-xl"
},
{
"attribute_code": "msrp_display_actual_price_type",
"value": "0"
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "size",
"value": "8"
}
]
},"saveOptions": true
}
The important thing to note is the product tag in the json.The swagger document help to idenity it. Here is the link to it: http://devdocs.magento.com/swagger/#!/catalogProductRepositoryV1/catalogProductRepositoryV1SavePost

Simple product with custom attributes (ex: remarks).
Just take note of the media_gallery_entries. Make sure to supply a valid base64_encoded_data image content and mime type.
URL: http://domain/index.php/rest/V1/products
METHOD: POST
HEADER:
application/json
Authorization: Bearer
POST DATA / RAW PAYLOAD:
{
"product": {
"sku": "TESTPRD002",
"name": "Women's Running - Pure Boost X Shoes",
"attribute_set_id": 4,
"price": 84,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2016-12-16 15:20:55",
"updated_at": "2016-12-16 15:20:23",
"weight": 2.5,
"extension_attributes": {
"stock_item": {
"item_id": 1,
"stock_id": 1,
"qty": 20,
"is_in_stock": true,
"is_qty_decimal": false
}
},
"product_links": [],
"options": [],
"media_gallery_entries": [
{
"media_type": "image",
"label": "Women's Running - Pure Boost X Shoes",
"position": 1,
"disabled": false,
"types": [
"image",
"small_image",
"thumbnail"
],
"content": {
"base64_encoded_data": "<ENCODED IMAGE DATA>",
"type": "image/jpeg",
"name": "TESTPRD002-01.jpg"
}
}
],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "description",
"value": "<p>Lightweight and sleek, these women's running shoes are fueled by boost™ energy. The low-profile runners blend an energy-returning boost™ midsole with a STRETCHWEB outsole for a cushioned ride with terrific ground-feel. They feature a breathable mesh upper with a sock-like fit that offers all-around support. With a full boost™ midsole that keeps every stride charged with light, fast energy, the shoe has an upper that hovers over a free-floating arch.</p>"
},
{
"attribute_code": "short_description",
"value": "<p>PURE BOOST X SHOES</p><p>NATURAL RUNNING SHOES WITH ARCH SUPPORT.</p>"
},
{
"attribute_code": "meta_title",
"value": "PURE BOOST X SHOES"
},
{
"attribute_code": "meta_keyword",
"value": "boost X, running, shoes, adidas"
},
{
"attribute_code": "meta_description",
"value": "NATURAL RUNNING SHOES WITH ARCH SUPPORT."
},
{
"attribute_code": "category_ids",
"value": [
"2", "3"
]
},
{
"attribute_code": "url_key",
"value": "womens-running-pure-boost-x-shoes"
},
{
"attribute_code": "tax_class_id",
"value": "1"
},
{
"attribute_code": "remarks",
"value": "Lorem ipsum.."
}
]
},
"saveOptions": true
}

Related

Magento 2 REST API : Product Price and Custom Attribute color are not reflecting in response

I am working on Magento2 [v2.4] Product integration use case.
I am creating a Product using REST API [postman] and observed that product price, color attributes are present in Request JSON but those are missing in Response received from Magento.
POST http://localhost/magento2/rest/V1/products
Request:
{
"product": {
"id": "2007",
"sku": "20210004",
"name": "Iphone 4",
"price": "400",
"status": 1,
"extension_attributes": {
"stock_item": {
"qty": 4,
"is_in_stock": "true"
}
},
"custom_attributes": [
{
"attribute_code": "color",
"value": 4
}
]
}
}
Response:
{
"id": 2007,
"sku": "20210004",
"name": "Iphone 4",
"attribute_set_id": 4,
"status": 1,
"visibility": 4,
"extension_attributes": {
"stock_item": {
"item_id": 18,
"product_id": 2007,
"stock_id": 1,
"qty": 4,
"is_in_stock": true,
"is_qty_decimal": false
}
},
"custom_attributes": [
{
"attribute_code": "options_container",
"value": "container2"
},
{
"attribute_code": "url_key",
"value": "iphone-4"
}
]
}
If you look at the request and response payload, you will see that Price and Color are there in request but somehow Magento did not honor those and got missed in response.
If I want to make this work, I had to re-send same payload again. Then I could see Price and Color in response.
Could you please suggest what is the issue here?
This is down to a bug in Magento (https://github.com/magento/magento2/issues/13639).
At the time of writing this should be fixed in 2.4.3. For versions 2.4.2 and lower you can add the following to your payload:
"type_id": "simple"

Google Places API: different output for textsearch and nearbysearch (formatted_address missing)

Want to use Google Places API nearby search for reverse lookup using coordinates.
I use nearbysearch endpoint.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.892674%2C151.200727&rankby=distance&type=bar&key=...
What is missing is formatted_address.
When trying to use textsearch instead, I do get formatted_address.
https://maps.googleapis.com/maps/api/place/textsearch/json?location=-33.892674%2C151.200727&rankby=distance&type=bar&key=...
Both return the same first place_id and look very similar.
Differences I noticed:
textsearch: formatted_address, empty opening_hours are included
nearbysearch: vicinity, scope, empty opening_hours are not included
How do I get formatted_address?
Update: I checked and both types should return the same fields.
details
When searching by distance & type I guess I could use either nearbysearch or textsearch, but when searching by prominence I will have to use nearbysearch as textsearch requires either type or query.
The query I would like to use (using nearbysearch, so missing formatted_address):
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.892674%2C151.200727&rankby=prominence&radius=500&key=...
Full output textsearch
{
"business_status": "OPERATIONAL",
"formatted_address": "7 Cope St, Redfern NSW 2016",
"geometry": {
"location": {
"lat": -33.892682,
"lng": 151.20075
},
"viewport": {
"northeast": {
"lat": -33.89147812010727,
"lng": 151.2019769298927
},
"southwest": {
"lat": -33.89417777989272,
"lng": 151.1992772701072
}
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
"id": "d8b3f319ad00fafd66527b248e450284a53386c2",
"name": "Arcadia",
"opening_hours": {
"open_now": true
},
"photos": [
{
"height": 3000,
"html_attributions": [
"Colin Hannah"
],
"photo_reference": "CmRaAAAAvnYeIFoCoiiQoyRqKQqpz1yJK71eXM3wYuCiTFTTtj3iwTiIW_z865AfLogqMiEyyBh5GqZoxAcHDmzyE8KelhGEh3C-ggm5LQDGK4zFWxdTNCholvXurq0ce7zLoztgEhBgklqzYxK5jhDhwqB5wuabGhQRUb448z5zV9l7EDSmXLMo5icv1w",
"width": 4000
}
],
"place_id": "ChIJ3Y3vQdqxEmsRTvCcbZnsYJ8",
"plus_code": {
"compound_code": "4642+W7 Redfern, New South Wales",
"global_code": "4RRH4642+W7"
},
"price_level": 2,
"rating": 4.5,
"reference": "ChIJ3Y3vQdqxEmsRTvCcbZnsYJ8",
"types": [
"bar",
"point_of_interest",
"establishment"
],
"user_ratings_total": 279
},
Full output nearbysearch:
{
"business_status": "OPERATIONAL",
"geometry": {
"location": {
"lat": -33.892682,
"lng": 151.20075
},
"viewport": {
"northeast": {
"lat": -33.8914789697085,
"lng": 151.2019760802915
},
"southwest": {
"lat": -33.89417693029149,
"lng": 151.1992781197085
}
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
"id": "d8b3f319ad00fafd66527b248e450284a53386c2",
"name": "Arcadia",
"opening_hours": {
"open_now": true
},
"photos": [
{
"height": 3024,
"html_attributions": [
"Milan"
],
"photo_reference": "CmRaAAAA7u4ZpZOddZ8ypuGySsQ1lG0HEX4Ke0DHeYtHYN1gEchx-yf1U-DVLNHVdmFVLoocHbMmWlMVCqcW9oMnpDC4-dw6ObZAovVQG90GpVD3sYeMEpzBB80yjhttjZ1lsIEdEhCzQJane_k8xy-HlI9ZlE9WGhQJ13KgR88Q239e8ocBCt5H0BgpKQ",
"width": 4032
}
],
"place_id": "ChIJ3Y3vQdqxEmsRTvCcbZnsYJ8",
"plus_code": {
"compound_code": "4642+W7 Redfern NSW, Australia",
"global_code": "4RRH4642+W7"
},
"price_level": 2,
"rating": 4.5,
"reference": "ChIJ3Y3vQdqxEmsRTvCcbZnsYJ8",
"scope": "GOOGLE", <===========
"types": [
"bar",
"point_of_interest",
"establishment"
],
"user_ratings_total": 279,
"vicinity": "7 Cope Street, Redfern" <===========
},
Comment from #MrUpsidown points to the documentation where it is stated:
A Text Search response is similar, except that it returns a formatted_address instead of a vicinity property
So, it seems that's just the way it is.
"formatted_address": "7 Cope St, Redfern NSW 2016",
vs
"vicinity": "7 Cope Street, Redfern"

Combine json response in nifi

We are calling invokehttp processes and getting response which json. Example
{
"id": "h569gcjhcm",
"doi": {
"id": "10.17632/h569gcjhcm.1",
"status": "allocated",
"prefix": "10.17632"
},
"name": "Data for: Flooding of the Caspian Sea at the intensification of Northern Hemisphere Glaciations",
"description": "Supplementary data for the Jeirankechmez section in Azerbaijan.\n\n- Appendix A contains all paleomagnetic data and interpretations of the Jeirankechmez section. This .dir file can be imported into the paleomagnetism.org webportal under \"Interpretation Portal\", \"Advanced Options\", \"Import Application Save\". For further details on the use of paleomagnetism.org please refer to the article by Koymans et al. (2016) - https://doi.org/10.1016/j.cageo.2016.05.007.\n- Appendix B contains the magnetic susceptibility data for the analysed samples, including geographic coordinates and stratigraphic levels.\n- Appendix C contains the 40Ar/39Ar data for the three analysed volcanic ash layers. ",
"version": 1,
"publish_date": "2019-01-29T12:51:38.090Z",
"data_licence": {
"id": "01d9c749-3c4d-4431-9df3-620b2dcfe144",
"short_name": "CC BY 4.0",
"full_name": "Creative Commons Attribution 4.0 International",
"description": "This dataset is licensed under a Creative Commons Attribution 4.0 International licence.\n\nWhat does this mean?\nYou can share, copy and modify this dataset so long as you give appropriate credit, provide a link to the CC BY license, and indicate if changes were made, but you may not do so in a way that suggests the rights holder has endorsed you or your use of the dataset. Note that further permission may be required for any content within the dataset that is identified as belonging to a third party.",
"url": "http://creativecommons.org/licenses/by/4.0",
"category": "Creative"
},
"contributors": [
{
"first_name": "Christiaan",
"last_name": "van Baak"
},
{
"first_name": "Marius",
"last_name": "Stoica"
},
{
"first_name": "Arjen",
"last_name": "Grothe"
},
{
"first_name": "Gareth",
"last_name": "Davies"
},
{
"profile_id": "72970719-95c8-341b-80d2-afa9e7154baf",
"first_name": "Wout",
"last_name": "Krijgsman"
},
{
"profile_id": "3a4bfe2c-4098-3859-9b88-789fa993e05a",
"first_name": "Keith",
"last_name": "Richards"
},
{
"profile_id": "f1660f3c-ebbd-3289-8240-1f4ea7913df4",
"first_name": "Klaudia",
"last_name": "Kuiper"
},
{
"first_name": "Elmira",
"last_name": "Aliyeva"
}
],
"versions": [
{
"version": 1,
"publish_date": "2019-01-29T12:51:38.090Z",
"available": true
}
],
"files": [
{
"filename": "Appendix_A_Jeirankechmez_pmag_interpretations.dir",
"id": "f2f4cba7-2411-4737-a9b2-f094db30dca1",
"content_details": {
"id": "994bc865-5300-4d76-a373-e528ccd830e8",
"sha256_hash": "2427c4b077372760973ce8224694f2a2ee5383c7f022ad818164d847a20e27cc",
"sha1_hash": "73792dc6d6eb2c1de1e04926ba5d4420dd0aaece",
"content_type": "application/x-director",
"size": 917022,
"created_date": "2019-01-03T00:00:00.000Z"
"download_expiry_time": "2019-01-29T13:52:25.729Z"
},
"metrics": {
"downloads": 0,
"previews": 0
}
},
{
"filename": "Appendix_B_Sample_locations_susceptibility.xlsx",
"id": "64241bf0-5279-49e8-a505-be9075b910e1",
"content_details": {
"id": "af8809d0-8e63-4599-abaa-e7af9ad39959",
"sha256_hash": "0588f44a0cbd477aa2798323e57ce0b2d4a118e767c0b1ffdc9eb1017e4d23c2",
"sha1_hash": "02e89f6f197ebf495e1e2c3d1aab250efc7545e7",
"content_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"size": 24770,
"created_date": "2019-01-03T00:00:00.000Z"
,
"download_expiry_time": "2019-01-29T13:52:25.732Z"
},
"metrics": {
"downloads": 0,
"previews": 0
}
},
{
"filename": "Appendix_C_ArAr_data.xlsx",
"id": "2e912027-ff3f-48ad-98b9-b643b59ba0e3",
"content_details": {
"id": "4960377c-060d-41f6-b7af-150617d8ebeb",
"sha256_hash": "235dc32c1e99f350ee5c99908a5f5d72d1aeeab02f78c2e0181d585bd1880fa6",
"sha1_hash": "6483156e4577948cac5d2679eee862c76faed1c9",
"content_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"size": 18510,
"created_date": "2019-01-03T00:00:00.000Z"
},
"metrics": {
"downloads": 0,
"previews": 0
}
}
],
"articles": [
{
"id": "10.1016/j.gloplacha.2019.01.007",
"title": "Flooding of the Caspian Sea at the intensification of Northern Hemisphere Glaciations",
"doi": "10.1016/j.gloplacha.2019.01.007",
"journal": {
"issn": "0921-8181",
"name": "Global and Planetary Change",
"url": "http://www.sciencedirect.com/science/journal/09218181"
}
}
],
"categories": [
{
"id": "http://com/vocabulary/OmniScience/Concept-170590667",
"label": "Geology"
},
{
"id": "http://data.elsevier.com/vocabulary/OmniScience/Concept-473860195",
"label": "Strontium Isotope"
}
],
"institutions": [ ],
"metrics": {
},
"available": true,
"related_links": [ ]
}
I am using $contributors.profile_id from above json to call new endpoint(invokeshttp) (https://api.xxx.com/profile/$.profile_id)
Json response for this
"contributors": [
{
“profile_id”:”cedferfiherhforhforf”
"first_name": “xxx”,
"last_name": "van Baak”,
“other_ids”:[] ,
“Other info”: “deeded” }
I have to call this endpoint depending upon number of object in contributor(let say we have 5 object in contributor ,so I have to call this endpoint 5 time)and combine these 5 response together
Then I have to merge the response(above response to the main response )
just an example:
EvaluateJsonPath to extract "id" into attribute, later join by this attribute
SplitJson to split your json by "contributors"
call endpoint
MergeContent merge by "id" and with count after SplitJson

Custom colors when generating a pie chart from JSON

I'm trying to create a pie chart with a custom set of colours using Am4Charts and the createFromConfig method.
I've followed the tutorial here but the chart keeps appearing with it's default color set.
Here is a sample of the JSON I've tried:
"innerRadius": 100,
"colors": {"list": ["#ff0000", "#00ff00", "#0000ff" ]},
"data": {
"0": {
"pot": "Within 8 days",
"value": "£111,119.70",
},
"1": {
"pot": "9 - 17 days",
"value": "£225,537.73"
},
"2": {
"pot": "18+ days",
"value": "£720,279.85"
}
},
"legend": [],
"xAxes": [
{
"type": "CategoryAxis",
"title": {
"text": "pot"
},
"dataFields": {
"category": "pot",
"title": {
"text": "Month"
}
},
"renderer": {
"labels": {
"rotation": 190,
"verticalCenter": "middle",
"horizontalCenter": "left"
}
}
}
],
"series": [
{
"type": "PieSeries",
"dataFields": {
"value": "value",
"category": "pot"
},
"ticks": {
"disabled": true
},
"labels": {
"disabled": true
},
}
],
Can somebody see where I've gone wrong?
Update 2:
Fixed in 4.0.0-beta.85.
Make sure you clear your browser cache after upgrading. And feel free to contact us again if you are still experiencing this issue.
Update 1:
Response from amchart contributor/CTO Martynas Majeris (https://github.com/martynasma):
Looks like there are two issues: documentation is wrong and there's a bug that prevents it from working :)
I updated the docs. It should say this:
{
// ...
"series": [{
// ...
"colors": {
"list": [
"#845EC2",
"#D65DB1",
"#FF6F91",
"#FF9671",
"#FFC75F",
"#F9F871"
]
}
}]
}
Also, fixed bug in dev version. New version will be released within 1-2 days.
Original
This might be a bug and I have opened an issue on amchart github. I will update this once I get a response: https://github.com/amcharts/amcharts4/issues/577
By the way, I do think your configuration JSON has couple issues:
data is an array, not an object
legend is an object, not an array
This is what I used to create the pie chart demo for the opened issue:
// Create chart instance in one go
let chart = am4core.createFromConfig({
"colors": {
"list": ["#ff0000","#00ff00", "#0000ff"]
},
// Create pie series
"series": [{
"colors": ["#ff0000","#00ff00", "#0000ff"],
"type": "PieSeries",
"dataFields": {
"value": "value",
"category": "pot"
}
}],
// Add data
"data": [{
"pot": "Within 8 days",
"value": "£111,119.70"
}, {
"pot": "9 - 17 days",
"value": "£225,537.73"
}, {
"pot": "18+ days",
"value": "£720,279.85"
}],
// Add legend
"legend": {},
"innerRadius": 100
}, "chart", am4charts.PieChart);

amcharts not rendering India map correctly in worldmap

While amcharts shows the India map correctly (showing the disputed regions as part of India) when displaying only India (http://jsfiddle.net/zxhseguw/5/)
"dataProvider": {
"map": "indiaLow",
"areas": [ {
"id": "IN-KA",
"value": 4447100,
}, {
"id": "IN-UP",
"value": 38763
}]
},
it shades it differently when rendering it on world map (http://jsfiddle.net/zxhseguw/6/)
"dataProvider": {
"map": "worldLow",
"areas": [ {
"id": "IN",
"value": 4447100,
}, {
"id": "AU",
"value": 387633
}]
},
I wonder, if there is a way to make it render India correctly, just like its possible in Google Charts by setting origin='India'
I'm assuming you're referring to the region around Kashmir, correct? Try using worldIndiaLow instead of worldLow, which includes more of that disputed area as part of India.
"dataProvider": {
"map": "worldIndiaLow",
"areas": [ {
"id": "IN",
"value": 4447100,
}, {
"id": "AU",
"value": 387633
}]
},
Updated fiddle

Resources