Color and size info

Does anyone know how I can access the Color and Size fields for the product variants in the Starter Site via the API

Answer this question

Color and size info

  • anjelinio

    This will not work for you.

    You will have to try something like the following

    public string GetProductVariantColumn(string columnName,string variantId)
    {
    string returnValue = String.Empty;

    ProductFamily pf = (ProductFamily)this.ProductItem;

    if (this.ProductItem != null && this.ProductItem.HasProperty(columnName) &&
    !this.ProductItem.IsPropertyNull(columnName))
    {
    returnValue = (string)pf.Variants[VariantId];
    }

    return returnValue;
    }

    where ProductItem is a Product


  • Foxtrot_Xray

    Iterate over skus and display the information. The closest I've come was seeing the values among ~60 others in Variant.DataRow.ItemArray. I'd like to see if a property exists and if it does retrieve the value.
  • Graeme Williams

    ProductFamily.Variants should give you access to all vairants of a ProductFamily.

    What are you trying to do


  • Bill PDX

    I think you just check the "Display on Site" checkbox if you want to display these properties in the ProductDetail control.

    If you want to actually access the values outside of this control for some sort of logical evaluation, you can retrieve these values from the ProductDetail class. If you have initialized a ProductDetail object on your current page (look at the Products.aspx page in the starter site for an example of this), you can use the method "GetProductColumn" and pass the string of the appropriate column in your database, which will return a string of the value from the database.

    For example, the OnInit method located in Products.aspx would appear (somewhat) as follows:

    protected override void OnInit(EventArgs e)
    {
    CatalogItemUri catalogUri;
    if (CatalogItemUri.TryCreate(Request.Url, out catalogUri) && catalogUri.Kind == CatalogItemUriKind.Product)
    {
    this.product = catalogUri.Product;
    }
    if (catalogUri != null)
    {
    this.ProductDetail.ProductItem = this.product;
    string size = this.ProductDetail.GetProductColumn("size");
    string color = this.ProductDetail.GetProductColumn("color");

    }
    }

    Going at this a bit more in-depth, when you downloaded the StarterSite, there should be a Compressed File within the download called CommerceComponents. Within this zip file the source code for ProductDetail, where you can actually see how the Commerce Server API is doing this.



  • bryceflorek

    I get the following error:

    Unable to cast object of type 'Microsoft.CommerceServer.Catalog.RegularProduct' to type 'Microsoft.CommerceServer.Catalog.ProductFamily'.

    The compiler also threw an error on this cast (string)pf.Variants[VariantId] so I changed it to pf.Variants[VariantId].ToString()


  • renyx

    No good for me.

    The code in ProductDetail is:

    public string GetProductColumn(string columnName)
    {
    string returnValue = String.Empty;
    if (this.ProductItem != null && this.ProductItem.HasProperty(columnName) &&
    !this.ProductItem.IsPropertyNull(columnName))
    {
    returnValue = (string)this.ProductItem[columnName];
    }

    return returnValue;
    }

    where ProductItem is a Product object.

    I implemented a similar function, but the property value is always empty.


  • Randine

    For the compiler error, it was a mistake in the code. It should have been

    returnValue = (string)pf.Variants[VariantId][columnName];

    As far as the other casting error goes, it looks like you are trying to get variant from a product and not from a productFamily. You can write your own function to get the product from the catalog and then get the variant Info required.

    ProductFamily pf = (ProductFamily)catalog.GetProduct(productId);

    Now use pf.Variants[Variant] to access the particular variant.


  • Grae Foster

    This was the only thing that yielded the desired results:

    string colorColumnName = "ProductColor";
    string sizeColumnName = "ProductSize";
    if (Variant.DataRow.Table.Columns.Contains(colorColumnName))
    color = Variant.DataRow.Table.Rows[0][colorColumnName].ToString();
    if (Variant.DataRow.Table.Columns.Contains(sizeColumnName))
    size = Variant.DataRow.Table.Rows[0][sizeColumnName].ToString();

    where Variant is an instance of a Variant.

    This call causes an invalid cast error at runtime:
    ProductFamily pf = (ProductFamily)catalog.GetProduct(productId);


  • Color and size info