Partilhar via


How to display product properties on your runtime site?

        When displaying products on your Commerce Server site it is often necessary to display additional information about that product. For eg when displaying a printer you might want to display the description and weight of the printer.You might also want to display the names of these properties in the language setting of the user's browser. Since different products in different catalogs can have different displayable properties, the product catalog system provides a way to programatically control the display of these properties on your runtime site in a truly multilingual fashion.
         A property definition has a built in attribute named DisplayOnSite which you can set to true for properties whose values you want to displayed on the site.You can also associate display names with property definitions in each of the languages your site supports.The display name for a property will be returned in a column named Displayname_<language_code> in the datatable returned by the GetPropertyAttributes method on the CatalogContext class.For eg the display name for a property in the de-DE (German) culture will be returned in a column named DisplayName_de-DE.This allows you to show Beschreibung as the display name for the Description property if the user's browser language is set to German. If you do not want to create language specific display names for a property, you can still store a display name for the property in the built in DisplayName column. You can use Business Desk to define display names for  properties or use the Interop or catalog web services in the feature pack to define the display names. In order to programatically define display names you should first add the DisplayName_<language code> column by calling the AddPropertyAttribute method and then set the display name using the SetPropertyAttributes method. 
         In your runtime site, on initialization you can use the GetPropertyAttributes method on the CatalogContext class and cache the list of properties which have the DisplayOnSite attribute set.You can either cache the property names as a StringCollection or cache the propertynames and display names in a DataTable.See this link for sample code. In order to display the properties for each product 
          CatalogContext catalogContext = CommerceContext.Current.CatalogSystem;
ProductCatalog productCatalog = catalogContext.GetCatalog("Catalog Name");
Product product = productCatalog.GetProduct("productId");
string culture = "en-US";
string displayNameColumn = string.Format("DisplayName_{0}", culture);
DataSet productProperties = product.GetProductProperties();
string definitionName = (string)productProperties.Tables[0].Rows[0]["DefinitionName"];
DataSet definitionProperties = catalogContext.GetDefinitionProperties(definitionName);
foreach(DataRow property in definitionProperties.Tables[0].Rows)
{
DataSet propertyAttributes = catalogContext.GetPropertyAttributes((string)property["PropertyName"]);
if ( (bool)propertyAttributes.Tables[0].Rows[0]["DisplayOnSite"] == true)
{
string propertyValueToDisplay =
productProperties.Tables[0].Rows[0][(string)property[”PropertyName”]].ToString() ;
string propertyDisplayName = (string)propertyAttributes.Tables[0].Rows[0][displayNameColumn] ;
                          // Display propertyDisplayName and propertyValueToDisplay on site
}
}
         Ofcourse and for performance reasons you can optimize the above sample code by caching the definitionProperties and propertyAttributes datasets.

Comments