Display Relative Number of Modifiers

Tags: v7

Considering the value of the setting IRestaurant.DisplayRelativeNumberOfModifiers, the number of portions of the modifier is calculated in string form in SyrveFront, which is displayed on the UI.

For example, a dish has a modifier Sour Cream that is part of a group of modifiers. For this modifier:

Then, when IRestaurant.DisplayRelativeNumberOfModifiers is set to true, it will display:

When IRestaurant.DisplayRelativeNumberOfModifiers is set to false, it will display the absolute amount of the modifier for the dish:

For the convenience of plugin developers and the ability to transfer the logic to their UI, we will provide an example of obtaining the modifier amount string here.

The method CalculateModifierAmountString takes

And it returns a string of the form <sign><number>, which should be displayed on the UI next to the name of the modifier, so the user sees on the screen <sign><number> <modifier name>.

public static string CalculateModifierAmountString(decimal modifierAmount, int defaultAmount, bool hideIfDefaultAmount, bool isPaid, bool isAmountIndependentOfParentAmount)
{
    // Setting the way to display the amount of group modifiers of the dish.
    var showDeltaAmount = PluginContext.Operations.GetHostRestaurant().DisplayRelativeNumberOfModifiers;

    // If the option "Amount is independent of the dish amount" is enabled, always write "+N".
    if (isAmountIndependentOfParentAmount)
        return $"+{modifierAmount}";

    // If the modifier is paid or we are showing the absolute amount of modifiers, write "×N".

    const string charX = "\u00D7";
    var multiplyAmountString = $"{charX}{modifierAmount}";

    if (isPaid || !showDeltaAmount)
        return multiplyAmountString;

    // Show the relative amount of modifiers.

    var deltaAmount = modifierAmount - defaultAmount;

    switch (deltaAmount)
    {
        case 1:
            return "+";
        case -1:
            return "-";
        case 0 when hideIfDefaultAmount:
            return string.Empty;
        case 0:
            return multiplyAmountString;
        default:
            return $"{deltaAmount:+#;-#;0}";
    }
}