Skip to content

Rest API and product custom options on Product creation #8349

Closed
@NotAndD

Description

@NotAndD

Preconditions

  1. Install Magento 2.1.3 with sample data.
  2. Set up Rest integration.

Steps to reproduce

  1. Create a Product using Rest API POST request at /V1/products. The product has a custom option, correctly set and the custom attributes required_options and has_options value set to "1". Like this example:
{
"product":{
"id":0,
"sku":"test_product",
"visibility":4,
"status":1,
"name":"A test product for custom options",
"price":"7.35",
"type_id":"simple",
"attribute_set_id":4,
"extension_attributes":{
"stock_item":{
"qty":"1",
"item_id":null,
"product_id":null,
"stock_id":null,
"is_in_stock":true,
"manage_stock":false
}
},
"custom_attributes":[
{"attribute_code":"cost",
"value":"7,3500"},
{"attribute_code":"category_ids",
"value":["6","53","6"]},
{"attribute_code":"options_container",
"value":"container2"},
{"attribute_code":"has_options",
"value":"1"},
{"attribute_code":"required_options",
"value":"1"}
],
"options":[
{
"product_sku":"test_product",
"option_id":0,
"title":"Scelta",
"type":"checkbox",
"sort_order":null,
"is_require":true,
"price_type":null,
"values":[
{
"title":"A3",
"sort_order":1,
"price":7.35,
"price_type":"fixed",
"sku":"A3"
},
{
"title":"A4",
"sort_order":1,
"price":9.45,
"price_type":"fixed",
"sku":"A4"
}
]}
]
},
"saveOptions":true
}
  1. Get as response for the Rest method called a succesfull response, containing the product created. Checking the frontend or backend, the product is correctly created. The response, for the example could be like this:
{
"id":87,
"sku":"test_product",
"name":"A test product for custom options",
"attribute_set_id":4,
"price":7.35,
"status":1,
"visibility":4,
"type_id":"simple",
"created_at":"2017-01-31 10:28:02",
"updated_at":"2017-01-31 10:28:02",
"extension_attributes":{
"stock_item":{
"item_id":82,
"product_id":87,
"stock_id":1,
"qty":1,
"is_in_stock":true,
"is_qty_decimal":false,
"show_default_notification_message":false,
"use_config_min_qty":true,
"min_qty":0,
"use_config_min_sale_qty":1,
"min_sale_qty":1,
"use_config_max_sale_qty":true,
"max_sale_qty":10000,
"use_config_backorders":true,
"backorders":0,
"use_config_notify_stock_qty":true,
"notify_stock_qty":1,
"use_config_qty_increments":true,
"qty_increments":0,
"use_config_enable_qty_inc":true,
"enable_qty_increments":false,
"use_config_manage_stock":true,
"manage_stock":true,
"low_stock_date":null,
"is_decimal_divided":false,
"stock_status_changed_auto":0
}
},
"product_links":[],
"options":[
{
"product_sku":"test_product",
"option_id":201,
"title":"Scelta",
"type":"checkbox",
"sort_order":0,
"is_require":true,
"values":[
{
"title":"A3",
"sort_order":1,
"price":7.35,
"price_type":"fixed",
"sku":"A3",
"option_type_id":578
},
{
"title":"A4",
"sort_order":1,
"price":9.45,
"price_type":"fixed",
"sku":"A4",
"option_type_id":579
}
]
}
],
"media_gallery_entries":[],
"tier_prices":[],
"custom_attributes":[
{
"attribute_code":"cost",
"value":"7.0000"
},
{
"attribute_code":"category_ids",
"value":["6","53"]
},
{
"attribute_code":"options_container",
"value":"container2"
},
{
"attribute_code":"required_options",
"value":"1"
},
{
"attribute_code":"has_options",
"value":"1"
},
{
"attribute_code":"url_key",
"value":"carta-fotocopie-disco"
},
{
"attribute_code":"tax_class_id",
"value":"2"
}
]
}
  1. Ask the same product with a GET Rest API request at /V1/products/{sku}

Expected result

  1. The given product, I mean the answer of the POST request of which I gave an example, should be the product really created on Magento and should be the same answered from the GET request. Custom options should be visible on frontend.

Actual result

  1. Custom options aren't visible on frontend. Asking the product with a GET Rest API request to /V1/products/{sku} gives a different answer from the response of the POST method. For example, something like this:
{
"id":87,
"sku":"test_product",
"name":"A test product for custom options",
"attribute_set_id":4,
"price":7.35,
"status":1,
"visibility":4,
"type_id":"simple",
"created_at":"2017-01-31 10:28:02",
"updated_at":"2017-01-31 10:30:28",
"extension_attributes":{
"stock_item":{
"item_id":82,
"product_id":87,
"stock_id":1,
"qty":1,
"is_in_stock":true,
"is_qty_decimal":false,
"show_default_notification_message":false,
"use_config_min_qty":true,
"min_qty":0,
"use_config_min_sale_qty":1,
"min_sale_qty":1,
"use_config_max_sale_qty":true,
"max_sale_qty":10000,
"use_config_backorders":true,
"backorders":0,
"use_config_notify_stock_qty":true,
"notify_stock_qty":1,
"use_config_qty_increments":true,
"qty_increments":0,
"use_config_enable_qty_inc":true,
"enable_qty_increments":false,
"use_config_manage_stock":true,
"manage_stock":true,
"low_stock_date":null,
"is_decimal_divided":false,
"stock_status_changed_auto":0
}},
"product_links":[],
"options":[
{
"product_sku":"test_product",
"option_id":202,
"title":"Scelta",
"type":"checkbox",
"sort_order":0,
"is_require":true,
"values":[
{
"title":"A3",
"sort_order":1,
"price":7.35,
"price_type":"fixed",
"sku":"A3",
"option_type_id":580
},
{
"title":"A4",
"sort_order":1,
"price":9.45,
"price_type":"fixed",
"sku":"A4",
"option_type_id":581
}]
}],
"media_gallery_entries":[],
"tier_prices":[],
"custom_attributes":[
{
"attribute_code":"cost",
"value":"7.0000"
},
{
"attribute_code":"category_ids",
"value":["6","53"]
},
{
"attribute_code":"options_container",
"value":"container2"
},
{
"attribute_code":"required_options",
"value":"0"
},
{
"attribute_code":"has_options",
"value":"0"
},
{
"attribute_code":"url_key",
"value":"carta-fotocopie-disco"
},
{
"attribute_code":"tax_class_id",
"value":"2"
}]
}

First of all, some options_ids are different from before, I read that option ids had an issue and the ids changed at every update of the product, but this seems like the ids change even on GET requests?

Second and most important, custom attributes of the product, has_options and required_options both now are set at "0", meaning that the custom options doesn't appear on frontend until this is changed. Interestingly enough, asking an update of the product with a PUT Rest API request at /V1/products/{sku} and sending exactly the same body of the POST request asked before, set the custom attributes correctly and the product custom options are finally visible on frontend.
Saving the product from web interface as admin without changing anything has the same effect, probably setting correctly the custom attributes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions