Get selected option in Shopping Cart

Evine Beursken asked on February 28, 2017 11:09

How can I access the selected color (which is an option) from a product?

What I have so far is:

OptionCategoryInfo category = OptionCategoryInfoProvider.GetOptionCategoryInfo("WalletColor_1", SiteContext.CurrentSiteName);

ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;
foreach(var c in cart.CartItems)
{
    productImage.ImageUrl = c.SKU.SKUImagePath;
    if(c.SKU.SKUOptionCategory != null)
    {
        //how can I get the selected color here?
        productColor.Text = c.SKU.SKUOptionCategory.CategoryName; 
    }
        productQty.Text = "test";
}

This code is in my ascx.cs file (a custom web part) to make sure the shopping cart shows a picture as well as the selected color of the product and the amount.

Any idea how to solve this?

Correct Answer

Suneel Jhangiani answered on February 28, 2017 12:09

Kentico actually stores Product Options as SKU's and hence it can be a little complex, however, I use the following function which returns the SKUID that defines the Option for which you can then get the SKUName which should have the color:

public static int GetOption(int skuid, int optionCategoryId)
{
    // get the variant options for the item
    var options = SKUInfoProvider.GetSKUs().WhereEquals("SKUOptionCategoryID", optionCategoryId);
    int optionSkuId;
    try
    {
        optionSkuId = VariantOptionInfoProvider.GetVariantOptions()
                        .WhereEquals("VariantSKUID", skuid)
                        .WhereIn("OptionSKUID", options).FirstObject.OptionSKUID;
    }
    catch //TODO: narrow down general catch
    {
        throw new ArgumentException("The product specified does not represent an option in the category.");
    }
    return optionSkuId;
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on February 28, 2017 11:14

Evine, have you checked Kentico API Examples ? This might provide with an idea.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.