Display Relative Number of Modifiers
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:
- the amount depends on the number of the main dish
- the modifier is free
- the default amount of the modifier is 3
Then, when IRestaurant.DisplayRelativeNumberOfModifiers is set to true, it will display:
- +2 Sour Cream, if the amount of the modifier is increased by 2
- - Sour Cream, if the amount is decreased by 1
When IRestaurant.DisplayRelativeNumberOfModifiers is set to false, it will display the absolute amount of the modifier for the dish:
- ×5 Sour Cream, if the amount of the modifier is increased by 2
- ×2 Sour Cream, if decreased by 1
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
decimal modifierAmount— the number of portions of the modifier,int defaultAmount— the default number of portions of the modifier,bool hideIfDefaultAmount— whether “Hide if default amount” is set for this group modifier,bool isPaid— whether the modifier is paid,bool isAmountIndependentOfParentAmount— whether “Amount is independent of the dish amount” is set for this modifier.
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}";
}
}