Working with Product Options and Variants in MVC

Andy Bochmann asked on October 29, 2019 00:12


I'm running Kentico 12 MVC and I'm trying to implement product options (e.g. accessories) into my ecommerce solution. The Dancing Goat website and the Kentico documentation provide a good example of how to use variants with Product Attributes, but I couldn't find an example for product options of type Products.

I entered the possible options into Kentico and the Kentico UI allow me to select all the different options as expected. My only trouble is getting it to work inside the MVC project.

I'm already able to query the options for a specific parent SKU:

var skuOptions = OptionCategoryInfoProvider.GetProductOptionCategories(parentSkuId, true, OptionCategoryTypeEnum.Products);

I have to call this method with the parent SKUID. I haven't figured out how to actually add the selected product variant with the selection option (accessory) to the shopping cart.

Is there an example somewhere?


Recent Answers

David te Kloese answered on October 29, 2019 09:55

There is api documentation for adding products with options to your cart: Adding products with product options to the current shoppingcart

You basically have to add the option's SKUID as an option to the cartitem:

var optionIds = new List<int>

var cartItemParams = new ShoppingCartItemParameters(product.SKUID, 1, optionIds);    
0 votesVote for this answer Mark as a Correct answer

Andy Bochmann answered on October 29, 2019 17:43

David, thank you. I've found the API documention yesterday after posting the question but it doesn't seem to work. I've checked the SKUIDs many times, they are correct, the cartItemParams object looks correct as well, but when I call shoppingService.AddItemToCart(cartItemParams) nothing gets added to the cart and the method returns null.

After digging through the Ecommerce module code of the CMS itself, I noticed, that instead of using the parent SKUID and the option SKUIDs list (like it implies in the documention), Kentico uses the variant SKUID and only puts the options of type Products into the list (in my case only one of the options).

// The optionId list includes no attributes and I use the variantSkuId
var options = optionIds.Select(c => new ShoppingCartItemParameters{ SKUID = c }).ToList();
var cartItemParams = new ShoppingCartItemParameters(variantSkuId, quantity, options);

Strangly, I also have to convert the List<int> to List<ShoppingCartItemParameters>. If I pass the list of SKUIDs directly to new ShoppingCartItemParameters(...) the ProductOptions list property of cartItemParams will be empty. It seems to ignore the list of option SKUIDs. However, when I use List<ShoppingCartItemParameters> it works.

So this works now :). I was happy when I found the API documention but I think it was misleading. I had no luck getting it to work with options of type Attribute and with the List<int>. Maybe it's a bug, I don't know. I was surprised that Kentico itself does it different (see method GetShoppingCartItemParameters() in ShoppingCartItemSelector.ascx.cs ).

I have one more question. Is it possible to use different quantities for products and product options? Let's say I order 1000 resistors and want them shipped as tape and reel. I this case my quantity of 1000 resistors would have 1 quantity of the tape and reel option. If the tape and reel can only hold 1000 parts, I will need 2 tape and reel options for 2000 resistors. I noticed, whenever I change the quantity of the product, the quantity of the option gets updated automatically. Is it somehow possible to modify the calculation of the option quantity?

0 votesVote for this answer Mark as a Correct answer

Andy Bochmann answered on October 29, 2019 18:12

Nevermind my last question. I think I can just override UpdateShoppingCartItemUnitsInternal in ShoppingCartItemInfoProvider to set the quantity of the option to a different number after updating the product count. That seems to work fine.

0 votesVote for this answer Mark as a Correct answer

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