Querying attributes
This topic contains the following:
- Querying a single attribute
- Querying all attributes
Querying a single attribute
To query a single attribute, you must perform the following:
- Get an instance of the catalog manager.
Get an instance of the CatalogManager object.
- Get the specified attribute.
To get the specified attribute, you must either call the GetProductAttribute method of the manager and pass the ID of the attribute as an argument, or call the GetProductAttributes method and filter the collection by one of the properties of the attribute (e.g. Title, Name).
Here are examples of querying a single attribute by ID, by title and by name:
Querying attribute by ID
public static ProductAttribute GetProductAttributeById(Guid id)
{
CatalogManager catalogManager = CatalogManager.GetManager();
ProductAttribute productAttribute = catalogManager.GetProductAttribute(id);
return productAttribute;
}
Querying attribute by title
public static ProductAttribute GetProductAttributeByTitle(string title)
{
CatalogManager catalogManager = CatalogManager.GetManager();
ProductAttribute productAttribute = catalogManager.GetProductAttributes().Where(a => a.Title == title).FirstOrDefault();
return productAttribute;
}
Querying attribute by name
public static ProductAttribute GetProductAttributeByName(string name)
{
CatalogManager catalogManager = CatalogManager.GetManager();
ProductAttribute productAttribute = catalogManager.GetProductAttributeByName(name);
return productAttribute;
}
Querying all attributes
To query all attributes, you must perform the following:
- Get an instance of the catalog manager.
Get an instance of the CatalogManager object.
- Get the attributes.
To get the attributes, you must call the GetAttributes method.
Here is a code example:
public static List<ProductAttribute> GetProductAttributes()
{
CatalogManager catalogManager = CatalogManager.GetManager();
List<ProductAttribute> productAttributes = catalogManager.GetProductAttributes().ToList();
return productAttributes;
}