Generating product variants including the price adjustments of product options
Currently, the product options price adjustments in Kentico Xperience E-commerce are applied to the resulting price only when used as standard options. This article will show you what customization needs to be done in order to take these adjustments into account also when generating the product variants in the administration interface.
Motivation
The product variants store their price independently from the base product and the options used to define these variants. Generating variants only copies the price of the base product by default.
If you need to define many product variants based on product options with varying prices, manually editing the prices could be a long and tedious task. Moreover, if the option adjustments are already defined, and you want the product variants to honor them, the requirement to take them into account when generating variants becomes perfectly legitimate.
Fortunately, there is an easy way to alter the variant generation page used in the administration interface, and with only a single line of custom code, make this work.
Solution
The custom code should be added to the UI page’s code-behind file, ~\CMS\CMSModules\Ecommerce\Pages\Tools\Products\Variant_New.aspx.cs,
in the method GenerateAndSave
All you need to do is add a line somewhere before line #822, to modify the newly created productVariant's Variant and its SKUPrice property with the attributes (options) price sum, like this:
productVariant.Variant.SKUPrice += productVariant.ProductAttributes.Sum(option => option.SKUPrice);
In case you expect using negative price adjustments with options (yes, these are allowed), you might want to extend the line a bit, and add a check to prevent the resulting price from being negative:
productVariant.Variant.SKUPrice = Math.Max(0m, productVariant.Variant.SKUPrice + productVariant.ProductAttributes.Sum(option => option.SKUPrice));
You can also download the zip archive with customized file (Kentico Xperience 13), which includes the custom code with comments. The custom code snippet from this file can be transferred to the compatible (see below) older version code file, but it needs to be merged because of some API and reference changes between Xperience 13.
Compatibility
This customization is applicable to the current Xperience release 13 (any development model) and also to the Kentico 11 and 12 supported versions. Earlier unsupported versions could work too, but were not checked.
Showcase
Following screenshots illustrate the prices of the generated variants, automatically adjusted by the amount specified in the option as a result of this customization. First, the price adjustments is defined in product options:
Together with base product price of $2.00, variants resulting from subset of option combinations...
... are then generated with the following prices:
As you can see, product variant prices now reflect both base price and the respective product options price adjustments.
Additional variants generation
You may have an additional requirement when generating additional variants. Adding the line as described above will preserve the modified price of existing variants when new variants are added in the future. This approach should likely suit the most common scenario. However, if you would like to re-generate the price of existing variants according to the options whenever new variants are added, you can add the same code (line) to the first foreach loop in the the GenerateAndSave method as well.
In both cases, before generating additional variants, please verify that the base product price is set according to your requirements. It may be altered by prior variants generation or any manual variant price change together with enabled setting "Keep the advertised price of the products according to their cheapest product variant". For more details about this setting, please refer to Configuring product settings section and E-commerce settings reference in the documentation.
Disclaimer
Please note that this code originates from Kentico Support and was not tested by our standard QA process. Although it was successfully tested by the author, we recommend taking backup of the modified file and thoroughly testing your implementation of the customization prior to any production deployment.