Component builder for: Compact card 1

Error executing template "Designs/Swift/Paragraph/Swift_ProductPrice.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_b228965c25fa44d385206d0754078f8b.Execute() in D:\dynamicweb.net\Solutions\CO3\granzow2023.cloud.dynamicweb-cms.com\Files\Templates\Designs\Swift\Paragraph\Swift_ProductPrice.cshtml:line 25
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
   at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   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 4 @{ 5 ProductViewModel product = null; 6 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 7 { 8 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 9 } 10 else if (Pageview.Page.Item["DummyProduct"] != null && 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 } 20 21 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", ""); 22 bool anonymousUser = Pageview.User == null; 23 bool isErpConnectionDown = !Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsWebServiceConnectionAvailable"]); 24 bool hidePrice = anonymousUsersLimitations.Contains("price") && anonymousUser || Pageview.AreaSettings.GetBoolean("ErpDownHidePrices") && isErpConnectionDown; 25 hidePrice = !hidePrice && product.Id != null && !Convert.ToBoolean(product.ProductFields["Custom_EnableAnonymousPurchase"]?.Value) && anonymousUser ? true : hidePrice; // #22458 26 hidePrice = Pageview.IsVisualEditorMode ? false : hidePrice; // #22458 27 28 bool productIsDiscontinued = product is object && product.Discontinued; 29 bool doNotShowPriceIfProductIsDiscontinued = Model.Item.GetBoolean("DoNotShowPriceIfProductIsDiscontinued"); 30 var isDiscontinued = productIsDiscontinued && doNotShowPriceIfProductIsDiscontinued; 31 } 32 33 @if (product is object && !hidePrice && !isDiscontinued) { 34 bool showInformativePrice = Model.Item.GetBoolean("ShowInformativePrice"); 35 string unitId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("UnitId")) ? Dynamicweb.Context.Current.Request.Form.Get("UnitId") : string.Empty; 36 37 string priceFontSize = Model.Item.GetRawValueString("PriceSize", "fs-2"); 38 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 39 string layout = Model.Item.GetRawValueString("Layout", "horizontal"); 40 string textAlign = horizontalAlign == "center" ? "text-center" : string.Empty; 41 textAlign = horizontalAlign == "end" ? "text-end" : textAlign; 42 43 horizontalAlign = horizontalAlign == "center" && layout == "horizontal" ? "justify-content-center" : horizontalAlign; 44 horizontalAlign = horizontalAlign == "end" && layout == "horizontal" ? "justify-content-end" : horizontalAlign; 45 horizontalAlign = horizontalAlign == "center" && layout == "vertical" ? "align-items-center" : horizontalAlign; 46 horizontalAlign = horizontalAlign == "end" && layout == "vertical" ? "align-items-end" : horizontalAlign; 47 48 string flexDirection = layout == "horizontal" ? string.Empty : "flex-column"; 49 string flexGap = layout == "horizontal" ? "gap-3" : string.Empty; 50 string order = layout == "horizontal" ? string.Empty : "order-2"; 51 string theme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("Theme")) ? "theme " + Model.Item.GetRawValueString("Theme").Replace(" ", "").Trim().ToLower() : ""; 52 theme = GetViewParameter("theme") != null ? GetViewParameterString("theme") : theme; 53 54 string contentPadding = Model.Item.GetRawValueString("ContentPadding", ""); 55 contentPadding = contentPadding == "none" ? "p-0" : contentPadding; 56 contentPadding = contentPadding == "small" ? "p-1 px-md-2 py-md-1" : contentPadding; 57 contentPadding = contentPadding == "large" ? "p-2 px-md-3 py-md-2" : contentPadding; 58 59 string showPricesWithVat = Pageview.Area.EcomPricesWithVat.ToLower(); 60 bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat); 61 62 string priceMin = ""; 63 string priceMax = ""; 64 65 string liveInfoClass = ""; 66 string productInfoFeed = ""; 67 bool isLazyLoadingForProductInfoEnabled = Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsLazyLoadingForProductInfoEnabled"]); 68 if (isLazyLoadingForProductInfoEnabled) 69 { 70 if (Dynamicweb.Context.Current.Items.Contains("ProductInfoFeed")) 71 { 72 productInfoFeed = Dynamicweb.Context.Current.Items["ProductInfoFeed"]?.ToString(); 73 if (!string.IsNullOrEmpty(productInfoFeed)) 74 { 75 productInfoFeed = $"data-product-info-feed=\"{productInfoFeed}\""; 76 } 77 } 78 liveInfoClass = "js-live-info"; 79 } 80 81 <div class="@textAlign @liveInfoClass item_@Model.Item.SystemName.ToLower()" data-product-id="@product.Id" data-variant-id="@product.VariantId" @productInfoFeed> 82 @if (showInformativePrice && product.PriceInformative.Price != 0) 83 { 84 <div class="opacity-50"> 85 <span>@Translate("RRP") </span> 86 <span class="text-decoration-line-through text-price">@product.PriceInformative.PriceFormatted</span> 87 </div> 88 } 89 <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"> 90 <span itemprop="priceCurrency" content="@product.Price.CurrencyCode" class="d-none"></span> 91 92 93 @if (showPricesWithVat == "false" && !neverShowVat) 94 { 95 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 96 { 97 <span itemprop="price" content="" class="d-none"></span> 98 <span class="text-decoration-line-through js-text-decoration-line-through opacity-75 me-3 text-price js-text-price d-none" data-show-if="LiveProductInfo.product.Price.Price != LiveProductInfo.product.PriceBeforeDiscount.Price"></span> 99 } 100 else 101 { 102 string beforePrice = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).PriceBeforeDiscount.PriceWithoutVatFormatted : product.PriceBeforeDiscount.PriceWithoutVatFormatted; 103 104 <span itemprop="price" content="@product.Price.PriceWithoutVat" class="d-none"></span> 105 if (product.Price.Price != product.PriceBeforeDiscount.Price) 106 { 107 <span class="text-decoration-line-through opacity-75 @order">@beforePrice</span> 108 } 109 } 110 } 111 else 112 { 113 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 114 { 115 <span itemprop="price" content="" class="d-none"></span> 116 <span class="text-decoration-line-through js-text-decoration-line-through opacity-75 me-3 text-price js-text-price d-none" data-show-if="LiveProductInfo.product.Price.Price != LiveProductInfo.product.PriceBeforeDiscount.Price"></span> 117 } 118 else 119 { 120 string beforePrice = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).PriceBeforeDiscount.PriceFormatted : product.PriceBeforeDiscount.PriceFormatted; 121 122 <span itemprop="price" content="@product.Price.Price" class="d-none"></span> 123 124 if (product.Price.Price != product.PriceBeforeDiscount.Price) 125 { 126 <span class="text-decoration-line-through opacity-75 @order"> 127 <span class="text-price">@beforePrice</span> 128 </span> 129 } 130 } 131 } 132 133 @if (showPricesWithVat == "false" && !neverShowVat) 134 { 135 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 136 { 137 <span class="text-price js-text-price"> 138 <span class="spinner-border" role="status"></span> 139 </span> 140 } 141 else 142 { 143 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceWithoutVatFormatted : product.Price.PriceWithoutVatFormatted; 144 145 if (product?.VariantInfo?.VariantInfo != null) 146 { 147 priceMin = product?.VariantInfo?.PriceMin?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithoutVatFormatted : ""; 148 priceMax = product?.VariantInfo?.PriceMax?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithoutVatFormatted : ""; 149 } 150 if (priceMin != priceMax) 151 { 152 price = priceMin + " - " + priceMax; 153 } 154 <span class="@theme @contentPadding"> 155 <span class="text-price">@price</span> 156 </span> 157 } 158 } 159 else 160 { 161 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 162 { 163 <span class="text-price js-text-price"> 164 <span class="spinner-border" role="status"></span> 165 </span> 166 } 167 else 168 { 169 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceFormatted : product.Price.PriceFormatted; 170 171 if (product?.VariantInfo?.VariantInfo != null) 172 { 173 priceMin = product?.VariantInfo?.PriceMin?.PriceFormatted != null ? product.VariantInfo.PriceMin.PriceFormatted : ""; 174 priceMax = product?.VariantInfo?.PriceMax?.PriceFormatted != null ? product.VariantInfo.PriceMax.PriceFormatted : ""; 175 } 176 if (priceMin != priceMax) 177 { 178 price = priceMin + " - " + priceMax; 179 } 180 <span class="@theme @contentPadding"> 181 <span class="text-price">@price</span> 182 </span> 183 } 184 } 185 186 @* Stock state for Schema.org, start *@ 187 @{ 188 Uri url = Dynamicweb.Context.Current.Request.Url; 189 } 190 191 <link itemprop="url" href="@url"> 192 193 @{ 194 bool IsNeverOutOfStock = product.NeverOutOfstock; 195 } 196 197 @if (IsNeverOutOfStock) 198 { 199 <span itemprop="availability" class="d-none">@Translate("Available in stock")</span> 200 } 201 else 202 { 203 if (product.StockLevel > 0) 204 { 205 <span itemprop="availability" class="d-none">InStock</span> 206 } 207 else 208 { 209 <span itemprop="availability" class="d-none">OutOfStock</span> 210 } 211 } 212 @* Stock state for Schema.org, stop *@ 213 214 </div> 215 216 @if (showPricesWithVat == "false" && !neverShowVat) 217 { 218 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 219 { 220 <small class="opacity-85 fst-normal js-text-price-with-vat d-none" data-suffix="@Translate("Incl. VAT")"></small> 221 } 222 else 223 { 224 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceWithVatFormatted : product.Price.PriceWithVatFormatted; 225 226 if (product?.VariantInfo?.VariantInfo != null) 227 { 228 priceMin = product?.VariantInfo?.PriceMin?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithVatFormatted : ""; 229 priceMax = product?.VariantInfo?.PriceMax?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithVatFormatted : ""; 230 } 231 if (priceMin != priceMax) 232 { 233 price = priceMin + " - " + priceMax; 234 } 235 <small class="opacity-85 fst-normal">@price @Translate("Incl. VAT")</small> 236 } 237 } 238 </div> 239 } 240 else if (Pageview.IsVisualEditorMode) 241 { 242 <div class="alert alert-dark m-0" role="alert"> 243 <span>@Translate("No products available")</span> 244 </div> 245 } 246