Class PriceViewModelExtensions
- Namespace
- Dynamicweb.Ecommerce.ProductCatalog
- Assembly
- Dynamicweb.Ecommerce.dll
Extension methods for PriceViewModel that provide analytics-friendly formatting, VAT label helpers, and conversion from the frontend price model.
public static class PriceViewModelExtensions
- Inheritance
-
PriceViewModelExtensions
- Inherited Members
Remarks
The most commonly used methods in Razor templates:
- ToStringInvariant(PriceViewModel) — culture-neutral price string for analytics / GTM data layers.
- TryGetVatLabel(PriceViewModel, out string) — renders the VAT label (e.g.
"incl. VAT") only when one applies. - GetVatMode(PriceViewModel) — returns an enum for switch-based VAT rendering logic.
Methods
GetVatLabel(PriceViewModel)
Returns the localizable English VAT label for the price (e.g. "incl. VAT").
public static string GetVatLabel(this PriceViewModel price)
Parameters
pricePriceViewModelThe price view model.
Returns
- string
One of
"incl. VAT","excl. VAT","excl. VAT (reverse charge)", or an empty string when GetVatMode(PriceViewModel) returns None. The returned strings are stable and intended to be wrapped in a Translate call in templates.
- See Also
GetVatMode(PriceViewModel)
Returns the VAT display mode for the price, useful for driving switch-based rendering logic.
public static PriceVatMode GetVatMode(this PriceViewModel price)
Parameters
pricePriceViewModelThe price view model.
Returns
- PriceVatMode
Value Condition None priceis null or VAT amount is 0.InclusiveVat ShowPricesWithVat is true. ExclusiveVatReverseCharge ReverseChargeForVat is true. ExclusiveVat Prices are shown ex-VAT without reverse charge.
- See Also
ToPriceViewModel(PriceViewModel)
Converts a PriceViewModel (used in non-ecommerce frontend contexts) into an ecommerce PriceViewModel so that the same Razor price-rendering helpers apply.
public static PriceViewModel? ToPriceViewModel(this PriceViewModel price)
Parameters
pricePriceViewModelThe frontend price view model to convert.
Returns
- PriceViewModel
A populated ecommerce PriceViewModel; null when
priceis null.
ToStringInvariant(PriceViewModel)
Converts the price to a string representation using the invariant culture ensuring . (dot) is used as decimal point symbol and always with 2 decimals and thousand separator is not used.
public static string ToStringInvariant(this PriceViewModel price)
Parameters
pricePriceViewModelThe PriceViewModel instance.
Returns
- string
A string representation of the price using e.g. '1234.45' or '2340.00'. If price is null, empty string is returned
Remarks
This method can be used to get the price formatted for e.g. analytics, google tag-manager and other services that require a specific format of prices. When called, the 'display price' is used - meaning that if the prices are currently showed with VAT, the price with VAT is used, otherwise the price without VAT is used.
ToStringInvariant(PriceViewModel, bool)
Converts the price to a string representation using the invariant culture ensuring . (dot) is used as decimal point symbol and always with 2 decimals and thousand separator is not used.
public static string ToStringInvariant(this PriceViewModel price, bool showWithVat)
Parameters
pricePriceViewModelThe PriceViewModel instance.
showWithVatboolif set to
truethe price with VAT is used, otherwise the price without VAT is used.
Returns
- string
A string representation of the price using e.g. '1234.45' or '2340.00'. If price is null, empty string is returned
Remarks
This method can be used to get the price formatted for e.g. analytics, google tag-manager and other services that require a specific format of prices.
TryGetVatLabel(PriceViewModel, out string)
Tries to get the VAT label for the price, returning false when no label applies. Prefer this over GetVatLabel(PriceViewModel) in templates to avoid rendering an empty span.
public static bool TryGetVatLabel(this PriceViewModel price, out string label)
Parameters
pricePriceViewModelThe price view model; may be null.
labelstringWhen this method returns true, contains the VAT label string (e.g.
"incl. VAT"); otherwise an empty string.
Returns
- bool
true when a non-empty VAT label exists for this price; false when
priceis null or VAT is zero.