Class ProductViewModelExtensions
- Namespace
- Dynamicweb.Ecommerce.ProductCatalog
- Assembly
- Dynamicweb.Ecommerce.dll
Extension methods for ProductViewModel providing asset helpers, variant utilities, price lookups, BOM configuration, related products, cart status checks, and product link generation.
public static class ProductViewModelExtensions
- Inheritance
-
ProductViewModelExtensions
- Inherited Members
Remarks
Add @using Dynamicweb.Ecommerce.ProductCatalog to your Razor template to access these methods.
The most commonly used methods in templates:
- GetProductLink(ProductViewModel, int, bool) — builds a URL to the product detail page.
- GetPrice(ProductViewModel) — returns a PriceInfoViewModel with before/after-discount prices.
- VariantGroups(ProductViewModel) — returns variant groups and options for rendering a variant selector.
- HasDiscount(ProductViewModel) — quick boolean check for showing "sale" badges.
- IsProductInCart(ProductViewModel, string?) — checks whether the product is already in the active cart.
Methods
BackInStockRegisteredForUser(ProductViewModel, string?, long)
Checks if the product is registered for back-in-stock notifications for the current user
public static bool BackInStockRegisteredForUser(this ProductViewModel product, string? unitId = null, long stocklocationId = 0)
Parameters
productProductViewModelThe product view model
unitIdstringThe unit ID of the product
stocklocationIdlongThe stock location ID
Returns
- bool
True if the product is registered for back-in-stock notifications, false otherwise
GetAssetsList(ProductViewModel, IEnumerable<string>, bool)
Gets product assets with only supported file formats and image patterns
public static IEnumerable<MediaViewModel> GetAssetsList(this ProductViewModel productViewModel, IEnumerable<string> selectedAssetsCategories, bool includeImagePatterns)
Parameters
productViewModelProductViewModelThe product view model
selectedAssetsCategoriesIEnumerable<string>The selected asset categories
includeImagePatternsboolFlag indicating whether to include image patterns
Returns
- IEnumerable<MediaViewModel>
The list of product assets
GetBomConfiguration(ProductViewModel)
Gets the bill of materials (BOM) configuration of the product
public static BomConfigurationViewModel GetBomConfiguration(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model
Returns
- BomConfigurationViewModel
The BOM configuration
GetPrice(ProductViewModel)
Gets the price information of the product
public static PriceInfoViewModel? GetPrice(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model
Returns
- PriceInfoViewModel
The price information
GetPrice(ProductViewModel, string?)
Gets the price information of the product for the specified unit
public static PriceInfoViewModel? GetPrice(this ProductViewModel productViewModel, string? unitId)
Parameters
productViewModelProductViewModelThe product view model
unitIdstringThe unit ID
Returns
- PriceInfoViewModel
The price information
GetPrice(ProductViewModel, string?, long)
Gets the price information of the product for the specified unit and stock location
public static PriceInfoViewModel? GetPrice(this ProductViewModel productViewModel, string? unitId, long stockLocationId)
Parameters
productViewModelProductViewModelThe product view model
unitIdstringThe unit ID
stockLocationIdlongThe stock location ID
Returns
- PriceInfoViewModel
The price information
GetProductDisplayGroupFieldsByGroupSystemNames(ProductViewModel, IEnumerable<string>, bool)
Gets the product display group fields by group system names
public static IEnumerable<FieldGroupViewModel> GetProductDisplayGroupFieldsByGroupSystemNames(this ProductViewModel productModel, IEnumerable<string> groupSystemNames, bool hideZeroValues = false)
Parameters
productModelProductViewModelThe product view model
groupSystemNamesIEnumerable<string>The group system names
hideZeroValuesboolFlag indicating whether to hide zero values
Returns
- IEnumerable<FieldGroupViewModel>
The product display group fields
GetProductLink(ProductViewModel, int, bool)
Gets the primary link to a given product using its default group id and that groups primary page id if that is specified - otherwise the specified page id.
public static string GetProductLink(this ProductViewModel product, int pageId, bool usePrimaryGroupPageId = true)
Parameters
productProductViewModelThe product view model
pageIdintThe page ID
usePrimaryGroupPageIdboolFlag indicating whether to use the primary group's page ID
Returns
- string
The primary link to the product
Remarks
If a product is member of 2 or more groups, the group specified as primary group or the default group in this shop context will be used to create the link. When the product is shown in 2 different group contexts, the link will be created using the group id specified in the parameter of the page and the link will be the same in both scenarios If a group context aware link is required, use the overload with the current group id.GetProductLink(ProductViewModel, int, string, bool)
GetProductLink(ProductViewModel, int, string, bool)
Gets the primary link to a given product using the current group context and that groups primary page id if that is specified.
public static string GetProductLink(this ProductViewModel product, int pageId, string currentGroupId, bool usePrimaryGroupPageId = true)
Parameters
productProductViewModelThe product view model
pageIdintThe page ID
currentGroupIdstringThe current group ID
usePrimaryGroupPageIdboolFlag indicating whether to use the primary group's page ID
Returns
- string
The link to the product using the group id of the current group context
Remarks
If a product is member of 2 or more groups and you want to show the product in the context of the current group and not its primary group, use this overload.
If the current group matches one of the groups the product belong to the link will be created using the group id specified in the currentGroupId parameter.
Otherwise the products subgroups are matched to see if one fits, and then the subgroup id is used if it matches the currentGroupId.
GetRelatedProducts(ProductViewModel)
Gets a collection of related products from all related groups
public static IEnumerable<ProductViewModel> GetRelatedProducts(this ProductViewModel product)
Parameters
productProductViewModelThe product view model
Returns
- IEnumerable<ProductViewModel>
The collection of related products
GetReplacementProduct(ProductViewModel)
Gets the full ProductViewModel for the product's replacement product, if it has one.
public static ProductViewModel? GetReplacementProduct(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model.
Returns
- ProductViewModel
The replacement product resolved with the same settings the product was created with (e.g. the cart's context for an order line product), or the current page's product view model settings when those are not available; otherwise, null.
Remarks
Requires ReplacementProduct to be among the product's filled properties.
GetSchemaOrgJsonLd(ProductViewModel)
Gets a serialized Schema.org Product JSON-LD string representing the product,
safe to embed in a <script type="application/ld+json"> block.
Only properties where the product has a value are included.
public static string? GetSchemaOrgJsonLd(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model.
Returns
- string
The JSON-LD string, or null if the product has no data to represent or SchemaOrgFeature is not active.
- See Also
GetSchemaOrgProduct(ProductViewModel)
Gets a Schema.org Product (https://schema.org/Product) model representing the product. Only properties where the product has a value are included.
public static SchemaOrgProduct? GetSchemaOrgProduct(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model.
Returns
- SchemaOrgProduct
The Schema.org Product model, or null if the product has no data to represent or SchemaOrgFeature is not active.
- See Also
GetVariantInfoOrEmpty(ProductViewModel)
Gets the product's variant information, or an empty instance when variant information was not populated.
public static VariantInfoViewModel GetVariantInfoOrEmpty(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model.
Returns
- VariantInfoViewModel
The populated variant information or an empty VariantInfoViewModel.
HasDiscount(ProductViewModel)
Determines whether the product has a discount (discount value > 0).
public static bool HasDiscount(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model.
Returns
- bool
True if a discount exists and its price value is greater than 0; otherwise false.
IsProductInCart(ProductViewModel, string?)
Checks if the product is in the cart
public static bool IsProductInCart(this ProductViewModel product, string? orderContextId = null)
Parameters
productProductViewModelThe product view model
orderContextIdstringThe order context ID
Returns
- bool
True if the product is in the cart, false otherwise
TryGetAssetsList(ProductViewModel, IEnumerable<string>, bool, out IEnumerable<MediaViewModel>?)
Tries to get product assets with only supported file formats and image patterns
public static bool TryGetAssetsList(this ProductViewModel productViewModel, IEnumerable<string> selectedAssetsCategories, bool includeImagePatterns, out IEnumerable<MediaViewModel>? assetsList)
Parameters
productViewModelProductViewModelThe product view model
selectedAssetsCategoriesIEnumerable<string>The selected asset categories
includeImagePatternsboolFlag indicating whether to include image patterns
assetsListIEnumerable<MediaViewModel>The list of product assets
Returns
- bool
True if the assets list is not empty, false otherwise
TryGetReplacementProduct(ProductViewModel, out ProductViewModel?)
Tries to get the full ProductViewModel for the product's replacement product.
public static bool TryGetReplacementProduct(this ProductViewModel productViewModel, out ProductViewModel? replacementProduct)
Parameters
productViewModelProductViewModelThe product view model.
replacementProductProductViewModelThe replacement product if found; otherwise, null.
Returns
- bool
True if the product has a replacement product that could be resolved; otherwise, false.
- See Also
VariantCombinations(ProductViewModel)
Gets the variant combinations of the product
public static List<string> VariantCombinations(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model
Returns
VariantGroups(ProductViewModel)
Gets the variant groups of the product
public static List<VariantGroupViewModel> VariantGroups(this ProductViewModel productViewModel)
Parameters
productViewModelProductViewModelThe product view model
Returns
- List<VariantGroupViewModel>
The list of variant groups