Overview
Once you assign an option to a product, you may find yourself needing to reverse that decision. This endpoint allows you to remove the assignment of an option from an existing product.
Requirements
The following pre-requisites will be necessary to accomplish the goals of this article:
- General understanding of REST and API’s
- Understanding of C# and/or JavaScript
- Visual Studio 2012 or newer
Getting Started
You should already have a project open and ready to accept code to allow you to use this REST API endpoint. You should also have one or more products created with a product option assignment, so that you can successfully remove an option assignment.
ProductOptionsUnassignFromProduct
This REST API endpoint allows you to remove the assignment of a product option from an existing product. This not only requires that you have an existing product to assign an option to, but that you also create or find a newly created option and have it already assigned to the product. In this example, we'll assume you've already used the ProductOptionsAssignToProduct endpoint to create a new option and then assign that option to the product.
Example URL
This is what the REST endpoint will look like if you are calling it using JavaScript. See our REST API URL documentation for more information.
http://example.com/DesktopModules/Hotcakes/API/rest/v1/productoptions/[OPTIONBVIN]/products/[PRODUCTBVIN]?key=[YOURKEY]&unassign=1
OPTIONBVIN is the option ID as described in the parameters below.
PRODUCTBVIN is the product ID as described in the parameters below.
HTTP Method
POST
Parameters
Parameter | Description |
optionbvin | This is the unique ID of the product option that you wish to assign to the product. |
productbvin | This is the unique ID of the product that you wish to assign an option to. |
unassign | This parameter should always be 1 if you intend to use this endpoint. |
Returns
If successful, this endpoint will return True in the ApiResponse.Content object. Otherwise, you should have one or more errors returned. If you receive a False response with no error, either the option ID or product ID doesn't exist, or the REST call was improperly formatted.
Example Code
The following code will allow you to successfully call this endpoint, provided that you have enabled it.
Project References
- Hotcakes.CommerceDTO
Import Namespaces
- Hotcakes.CommerceDTO.v1
- Hotcakes.CommerceDTO.v1.Catalog
- Hotcakes.CommerceDTO.v1.Client
string url = "http://example.com";
string key = "YOUR-API-KEY";
Api proxy = new Api(url, key);
// create a new instance of a product option
var option = new OptionDTO();
// populate the product option object with minimal information
option.Name = "Color";
option.OptionType = OptionTypesDTO.DropDownList;
option.Items.Add(new OptionItemDTO
{
Name = "Red"
});
option.Items.Add(new OptionItemDTO
{
Name = "White"
});
option.Items.Add(new OptionItemDTO
{
Name = "Blue"
});
option.Settings.Add(new OptionSettingDTO
{
Key = "ERPID",
Value = "1234567890"
});
// call the API to create the new product option
ApiResponse<OptionDTO> optionResponse = proxy.ProductOptionsCreate(option);
// the ID of the option to assign to the product
var optionId = optionResponse.Content.Bvin;
// specify the ID of the product to assign the option to
var productId = "06f2347e-ee44-4c54-a530-61f6848f2bea";
// call the API to assign the option to the product
ApiResponse<bool> response = proxy.ProductOptionsAssignToProduct(optionId, productId, false);
// call the API to remove the option assignment from the product
ApiResponse<bool> unassignResponse = proxy.ProductOptionsUnassignFromProduct(optionId, productId);
// your domain name
var url = "http://example.com";
// the path where the API is
var apiPath = "/DesktopModules/Hotcakes/API/rest/v1/";
// endpoint as shown in the documentation
var endPoint = "productoptions";
// get this from the store admin
var apiKey = "YOUR-API-KEY";
$(document).ready(function() {
// create a new instance of a product option
var option = {
"Name": "Color",
"OptionType": 100,
"Items": [
{
"Name": "Red"
},
{
"Name": "White"
},
{
"Name": "Blue"
}
],
"Settings": [
{
"Key": "ERPID",
"Value": "123456789"
}
]
};
// call the API to create the new product option
$.ajax({
dataType: "json",
url: url + apiPath + endPoint + "?key=" + apiKey,
data: JSON.stringify(option),
type: 'POST',
success: function (data) {
// assign the new option to the product
AssignOptionToProduct(data.Content.Bvin);
},
error: function (jqXHR, textStatus) {
// do something
}
});
});
function AssignOptionToProduct(optionId) {
// set to true if you want to generate all possible variants after adding this option
var generateVariants = "0";
// specify the ID of the product to create variants for
var productId = "06f2347e-ee44-4c54-a530-61f6848f2bea";
// call the API to generate the variants
$.ajax({
dataType: "json",
url: url + apiPath + endPoint + "/" + optionId + "/products/" + productId + "?key=" + apiKey + "&generatevariants=" + generateVariants,
type: 'POST',
success: function (data) {
// unassign the option from the product
UnassignOptionFromProduct(optionId);
},
error: function (jqXHR, textStatus) {
$('#lblStatus').html("Error!");
printResults("// " + textStatus);
}
});
}
function UnassignOptionFromProduct(optionId) {;
// specify the ID of the product to create variants for
var productId = "06f2347e-ee44-4c54-a530-61f6848f2bea";
// call the API to remove the assignment
$.ajax({
dataType: "json",
url: url + apiPath + endPoint + "/" + optionId + "/products/" + productId + "?key=" + apiKey + "&unassign=1",
type: 'POST',
success: function (data) {
$('#lblStatus').html("Success!");
var json = JSON.stringify(data);
printResults(json);
},
error: function (jqXHR, textStatus) {
$('#lblStatus').html("Error!");
printResults("// " + textStatus);
}
});
}
{
"Errors":[
],
"Content":true
}
Need More Help?
Do you need more assistance with this article? Please review your support options.