Product List Card
Error executing template "Designs/Swift-v2/Paragraph/Swift-v2_ProductPrice.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_7dea2f317f9a4e5684af4984f6751ae8.ExecuteAsync()
   at RazorEngine.Templating.TemplateBase.Run(ExecuteContext context, TextWriter reader)
   at RazorEngine.Templating.RazorEngineCore.RunTemplate(ICompiledTemplate template, TextWriter writer, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineService.Run(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.DynamicWrapperService.Run(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.Run(IRazorEngineService service, String name, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass23_0.<Run>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at RazorEngine.Templating.RazorEngineServiceExtensions.Run(IRazorEngineService service, String name, Type modelType, Object model, DynamicViewBag viewBag)
   at Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
   at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
   at Dynamicweb.Rendering.Template.RenderRazorTemplate()

1 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel> 2 @using Dynamicweb.Ecommerce.ProductCatalog 3 @using Dynamicweb.Ecommerce.Products 4 5 @{ 6 ProductViewModel product = null; 7 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 8 { 9 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 10 } else if (!string.IsNullOrEmpty(Pageview.Page.Item["DummyProduct"]?.ToString()) && Pageview.IsVisualEditorMode) 11 { 12 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page); 13 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel(); 14 15 if (productList?.Products is object) 16 { 17 product = productList.Products[0]; 18 } 19 } else if (Pageview.IsVisualEditorMode) { 20 product = new ProductViewModel(); 21 product.Price = new PriceViewModel() { 22 Price = 99, 23 PriceFormatted = "99 " + Pageview.Area.EcomCurrencyId, 24 PriceWithoutVat = 99, 25 PriceWithoutVatFormatted = "99 " + Pageview.Area.EcomCurrencyId, 26 PriceWithVat = 99, 27 PriceWithVatFormatted = "99 " + Pageview.Area.EcomCurrencyId 28 }; 29 product.PriceInformative = new PriceViewModel() { 30 Price = 49, 31 PriceFormatted = "49 " + Pageview.Area.EcomCurrencyId, 32 PriceWithoutVat = 49, 33 PriceWithoutVatFormatted = "49 " + Pageview.Area.EcomCurrencyId, 34 PriceWithVat = 49, 35 PriceWithVatFormatted = "49 " + Pageview.Area.EcomCurrencyId 36 }; 37 product.PriceBeforeDiscount = new PriceViewModel() { 38 Price = 199, 39 PriceFormatted = "199 " + Pageview.Area.EcomCurrencyId, 40 PriceWithoutVat = 199, 41 PriceWithoutVatFormatted = "199 " + Pageview.Area.EcomCurrencyId, 42 PriceWithVat = 199, 43 PriceWithVatFormatted = "99 " + Pageview.Area.EcomCurrencyId 44 }; 45 } 46 47 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", ""); 48 bool anonymousUser = Pageview.User == null; 49 bool hidePrice = anonymousUsersLimitations.Contains("price") && anonymousUser; 50 51 bool productIsDiscontinued = product is object && product.Discontinued; 52 bool doNotShowPriceIfProductIsDiscontinued = Model.Item.GetBoolean("DoNotShowPriceIfProductIsDiscontinued"); 53 var isDiscontinued = productIsDiscontinued && doNotShowPriceIfProductIsDiscontinued; 54 55 string priceType = string.Empty; 56 if (Dynamicweb.Context.Current.Items.Contains("PriceType")) 57 { 58 priceType = Dynamicweb.Context.Current.Items["PriceType"].ToString().ToLower(); 59 } 60 61 string isPriceRequestfieldValue = product.ProductFields["Prisfrfrgan_och_kontakt"].Value.ToString(); 62 bool isPriceRequest = isPriceRequestfieldValue.Equals("True", StringComparison.OrdinalIgnoreCase); 63 if (isPriceRequest) { 64 return; 65 } 66 } 67 68 @if (product is object && !hidePrice && !isDiscontinued && priceType != "fixedprice") { 69 bool showInformativePrice = Model.Item.GetBoolean("ShowInformativePrice"); 70 string unitId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("UnitId")) ? Dynamicweb.Context.Current.Request.Form.Get("UnitId") : string.Empty; 71 72 string priceFontSize = Model.Item.GetRawValueString("PriceSize", "fs-2"); 73 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 74 string layout = Model.Item.GetRawValueString("Layout", "horizontal"); 75 string textAlign = horizontalAlign == "center" ? "text-center" : string.Empty; 76 textAlign = horizontalAlign == "end" ? "text-end" : textAlign; 77 78 horizontalAlign = horizontalAlign == "center" && layout == "horizontal" ? "justify-content-center" : horizontalAlign; 79 horizontalAlign = horizontalAlign == "end" && layout == "horizontal" ? "justify-content-end" : horizontalAlign; 80 horizontalAlign = horizontalAlign == "center" && layout == "vertical" ? "align-items-center" : horizontalAlign; 81 horizontalAlign = horizontalAlign == "end" && layout == "vertical" ? "align-items-end" : horizontalAlign; 82 83 string flexDirection = layout == "horizontal" ? string.Empty : "flex-column"; 84 string flexGap = layout == "horizontal" ? "gap-3" : string.Empty; 85 string order = layout == "horizontal" ? string.Empty : "order-2"; 86 87 string showPricesWithVat = Dynamicweb.Ecommerce.Common.Context.DisplayPricesWithVat.ToString(); 88 bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat); 89 90 string priceMin = ""; 91 string priceMax = ""; 92 93 @* Hide price is product has variants and is not a variant *@ 94 bool isVariant = !string.IsNullOrEmpty(product.VariantId); 95 bool hasVariants = product.VariantInfo.VariantInfo != null; 96 if (!isVariant && hasVariants) { 97 return; 98 } 99 100 <div class="@textAlign item_@Model.Item.SystemName.ToLower()" data-product-id="@product.Id" data-variant-id="@product.VariantId"> 101 @if (showInformativePrice && product.PriceInformative.Price != 0) 102 { 103 <div class="opacity-50"> 104 <span>@Translate("RRP") </span> 105 <span class="text-decoration-line-through text-price">@product.PriceInformative.PriceFormatted</span> 106 </div> 107 } 108 <div class="@priceFontSize m-0 d-flex flex-wrap @flexDirection @flexGap @horizontalAlign" style="row-gap: 0 !important" itemprop="offers" itemscope itemtype="https://schema.org/Offer"> 109 <span itemprop="priceCurrency" content="@product.Price.CurrencyCode" class="d-none"></span> 110 111 112 @if (showPricesWithVat == "false" && !neverShowVat) 113 { 114 string beforePrice = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).PriceBeforeDiscount.PriceWithoutVatFormatted : product.PriceBeforeDiscount.PriceWithoutVatFormatted; 115 116 <span itemprop="price" content="@product.Price.PriceWithoutVat" class="d-none"></span> 117 if (product.Price.Price != product.PriceBeforeDiscount.Price) 118 { 119 <span class="text-decoration-line-through opacity-75 @order">@beforePrice</span> 120 } 121 } 122 else 123 { 124 string beforePrice = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).PriceBeforeDiscount.PriceFormatted : product.PriceBeforeDiscount.PriceFormatted; 125 126 <span itemprop="price" content="@product.Price.Price" class="d-none"></span> 127 128 if (product.Price.Price != product.PriceBeforeDiscount.Price) 129 { 130 <span class="text-decoration-line-through opacity-75 @order"> 131 <span class="text-price">@beforePrice</span> 132 </span> 133 } 134 } 135 @if (showPricesWithVat == "false" && !neverShowVat) 136 { 137 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceWithoutVatFormatted : product.Price.PriceWithoutVatFormatted; 138 139 if (product?.VariantInfo?.VariantInfo != null) 140 { 141 priceMin = product?.VariantInfo?.PriceMin?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithoutVatFormatted : ""; 142 priceMax = product?.VariantInfo?.PriceMax?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithoutVatFormatted : ""; 143 } 144 if (priceMin != priceMax) 145 { 146 price = priceMin + " - " + priceMax; 147 } 148 149 <span class="text-price">@price</span> 150 } 151 else 152 { 153 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceFormatted : product.Price.PriceFormatted; 154 155 if (product?.VariantInfo?.VariantInfo != null) 156 { 157 priceMin = product?.VariantInfo?.PriceMin?.PriceFormatted != null ? product.VariantInfo.PriceMin.PriceFormatted : ""; 158 priceMax = product?.VariantInfo?.PriceMax?.PriceFormatted != null ? product.VariantInfo.PriceMax.PriceFormatted : ""; 159 } 160 if (priceMin != priceMax) 161 { 162 price = priceMin + " - " + priceMax; 163 } 164 165 <span class="text-price">@price</span> 166 } 167 168 @* Stock state for Schema.org, start *@ 169 @{ 170 Uri url = Dynamicweb.Context.Current.Request.Url; 171 } 172 173 <link itemprop="url" href="@url"> 174 175 @{ 176 bool IsNeverOutOfStock = product.NeverOutOfstock; 177 } 178 179 @if (IsNeverOutOfStock) 180 { 181 <span itemprop="availability" class="d-none">@Translate("Available in stock")</span> 182 } 183 else 184 { 185 if (product.StockLevel > 0) 186 { 187 <span itemprop="availability" class="d-none">InStock</span> 188 } 189 else 190 { 191 <span itemprop="availability" class="d-none">OutOfStock</span> 192 } 193 } 194 @* Stock state for Schema.org, stop *@ 195 196 </div> 197 198 @if (showPricesWithVat == "false" && !neverShowVat) 199 { 200 if (!Pageview.IsVisualEditorMode) 201 { 202 <small class="opacity-85 fst-normal js-text-price-with-vat d-none" data-suffix="@Translate("Incl. VAT")"></small> 203 } 204 else 205 { 206 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceWithVatFormatted : product.Price.PriceWithVatFormatted; 207 208 if (product?.VariantInfo?.VariantInfo != null) 209 { 210 priceMin = product?.VariantInfo?.PriceMin?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithVatFormatted : ""; 211 priceMax = product?.VariantInfo?.PriceMax?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithVatFormatted : ""; 212 } 213 if (priceMin != priceMax) 214 { 215 price = priceMin + " - " + priceMax; 216 } 217 <small class="opacity-85 fst-normal">@price @Translate("Incl. VAT")</small> 218 } 219 } 220 </div> 221 } 222 else if (Pageview.IsVisualEditorMode) 223 { 224 <div class="alert alert-dark m-0" role="alert"> 225 <span>@Translate("No products available")</span> 226 </div> 227 } 228