Check stock availability by variants

Geoffrey Gadisseur asked on January 17, 2024 08:40


For a client, we need to hide or not a button to add product in the cart. We need to do it in a transformation. Actually, we use this condition to hide the button and show a text: "IfCompare(Eval("SKUAvailableItems"), 0, false, true)"

It work fine when the product stock is track "ByProduct". But when the stock is track "ByVariant", this condition is always true and the button is always hide.

How can we do this when the stock is track "ByVariant" ?

Thanks for your help

Correct Answer

Zdeněk Cetkovský answered on February 1, 2024 15:05

Actually, there are many ways to do it, it may just make the transformation code less "pretty". You can still use collection operation like Any(). For example, prepare a bool variable in the transformation inline code and use it to output desired message. A following code snippet I tried in the ASCX transformation can get you the combined stock availability output, considering all standard cases (stock tracking disabled / enabled for main product / by variants):

   bool stockNotTracked = SKU.SKUTrackInventory == CMS.Ecommerce.TrackInventoryTypeEnum.Disabled;

   bool stockAvailable = SKU.SKUAvailableItems > 0;

   bool stockVariantAvailable = false;

   if (SKU.SKUTrackInventory == CMS.Ecommerce.TrackInventoryTypeEnum.ByVariants) {
      List<CMS.Ecommerce.SKUInfo> variants = CMS.Ecommerce.VariantHelper.GetVariants(SKU.SKUID).ToList();
      stockVariantAvailable = variants.Any(variant => variant.SKUAvailableItems > 0);

   bool inStock = stockNotTracked || stockAvailable || stockVariantAvailable;

Hope this will be helpful.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Zdeněk Cetkovský answered on January 29, 2024 16:48 (last edited on February 1, 2024 14:49)


A simple check for SKUAvailableItems of the parent product would indeed return 0 (aka not available) if the tracking and quantites are set for variants, even when there are some variants existing. It works automatically only in the opposite direction, when you query the available items on the product variant and the tracking is set "By product".

Now, how to approach you aim - an inspiration can be found e.g. in the default control ShoppingCartItemSelector code - in Kentico 12 it is \CMS\CMSModules\Ecommerce\Controls\ProductOptions\ShoppingCartItemSelector.ascx.cs, method private void SetStockInformation().

Case branch for when the inventory type is TrackInventoryTypeEnum.ByVariants checks for any available items in all variants of the product:

Variants.Exists(variant => variant.Variant.SKUAvailableItems > 0)

This collection used in the control is not available in transformation directly, so it might be worth creating a custom function for this purpose, accepting the SKUInfo object, allowing easier control over the implementation.

0 votesVote for this answer Mark as a Correct answer

Geoffrey Gadisseur answered on January 30, 2024 17:18


Thanks for your reply.

The problem is that for some reason, we cannot use custom function in this project.

How can we do it with a loop structure directly in the transformation code ?

0 votesVote for this answer Mark as a Correct answer

Geoffrey Gadisseur answered on February 2, 2024 09:56


Thanks you. It works fine for me

0 votesVote for this answer Mark as a Correct answer

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