Problem adding product with options and variants to cart

Kyle Sands asked on March 17, 2020 05:42

Hello there!

I've been working on a website with a fairly complex product structure and have just gotten around to hooking up the cart. However, I'm running into problems with adding products that have both variants and product options (of the product variety) to the cart. My products have variants based on color, and product options based on fastener type.

I've been following the documentation closely and have been doing something along these lines:

var optionIds = new List<int>
var cartItemParams = new ShoppingCartItemParameters(product.SKUID, 1, optionIds);

IShoppingService shoppingService = Service.Resolve<IShoppingService>();


I've run numerous tests and can't seem to get a product with options and variants to add to the cart. I've tried passing the main product SKUID in with just the fastener option ID, I've tried passing it in with the fastener option and color option ids, and I've tried to do both of those while passing the variant SKUID in as the product as well. Every time I try, nothing is added to the cart.

Nothing seems to work until I remove either the product option category from the product or the option that determines the variants, then it works like expected. Does anyone have any info on how this would work, or a better way to figure out what is happening behind the scenes? Not knowing why it is failing is maddening.

Correct Answer

Kyle Sands answered on March 17, 2020 15:49

For anyone having this problem, I found the answer here

The relevant parts of the answer: 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.

The documentation really should be updated to cover this, because it is almost impossible to figure out on your own.

0 votesVote for this answer Unmark Correct answer

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