Skip to content

Instantly share code, notes, and snippets.

@Mozu-CS
Last active December 9, 2015 07:49
Show Gist options
  • Save Mozu-CS/c0361395569ed7dfd9f4 to your computer and use it in GitHub Desktop.
Save Mozu-CS/c0361395569ed7dfd9f4 to your computer and use it in GitHub Desktop.
Creating a Mozu Product Attribute List Option
public void Add_A_Product_Attribute_List_Option()
{
//enter your context info
var apiContext = new Mozu.Api.ApiContext(tenantId: 14617, masterCatalogId: 1, catalogId: 1);
//create a new attribute resource
var attributeResource = new Mozu.Api.Resources.Commerce.Catalog.Admin.Attributedefinition.AttributeResource(apiContext);
//define the attribute name
var attributeName = "Purse-Size";
//Add Your Code: define new attribute object
var attribute = new Mozu.Api.Contracts.ProductAdmin.Attribute()
{
AdminName = attributeName,
AttributeCode = attributeName,
DataType = "String",
InputType = "List",
IsExtra = false,
IsOption = true,
IsProperty = false,
Namespace = "Tenant",
Content = new AttributeLocalizedContent()
{
Name = attributeName,
},
ValueType = "Predefined",
SearchSettings = new AttributeSearchSettings()
{
SearchDisplayValue = true,
SearchableInStorefront = true,
SearchableInAdmin = true
},
};
//build fully qualified name
attribute.AttributeFQN = string.Format("{0}~{1}",
attribute.Namespace,
attributeName);
//define attribute vocabulary values
attribute.VocabularyValues = new System.Collections.Generic.List<AttributeVocabularyValue>();
//vocabulary values to add
var sizes = "Petite|Classic|Alta".Split('|');
foreach (var value in sizes)
{
attribute.VocabularyValues.Add(new AttributeVocabularyValue()
{
Value = value,
Content = new Mozu.Api.Contracts.ProductAdmin.AttributeVocabularyValueLocalizedContent()
{
LocaleCode = "en-US",
StringValue = value
}
});
}
//check is attribute already exists, return back only the attributeFQN as a responsefield and not the entire object
var existingAttributes = attributeResource.GetAttributesAsync(filter: string.Format("AttributeCode sw '{0}'", attributeName.ToLower()), responseFields: "AttributeFQN").Result;
//check if attribute already exists
if (existingAttributes.TotalCount == 0)
{
//Add Your Code: add new attribute
var createdAttribute = attributeResource.AddAttributeAsync(attribute).Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment