Komponent editor til: Kontra Related Products Card

Tea Symphony Classic Tea Assortment Box Rainforest Alliance
Tea Symphony Classic Tea Assortment Box Rainforest Alliance
Error executing template "/Designs/Swift/Paragraph/Swift_ProductPrice_TeOgKaf_custom.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at Dynamicweb.Ecommerce.ProductCatalog.ProductViewModelExtensionMethods.GetPriceForProduct(ProductViewModel productViewModel, String unitId, Int64 stockLocationId)
   at Dynamicweb.Ecommerce.ProductCatalog.ProductViewModelExtensionMethods.GetPrice(ProductViewModel productViewModel)
   at CompiledRazorTemplates.Dynamic.RazorEngine_88b82235ddb24e1d938a5ef8c58e7b90.Execute() in D:\Solution\BKI LIVE\Files\Templates\Designs\Swift\Paragraph\Swift_ProductPrice_TeOgKaf_custom.cshtml:line 31
   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 @using S_DW_BKI_Swift.CustomModules.Helpers 4 @using Dynamicweb.Ecommerce.DynamicwebLiveIntegration 5 @using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products 6 @using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration 7 @{ 8 ProductViewModel product = null; 9 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 10 { 11 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 12 } 13 else if (Pageview.Item["DummyProduct"] != null) 14 { 15 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page); 16 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel(); 17 18 if (productList?.Products is object) 19 { 20 product = productList.Products[0]; 21 } 22 } 23 //custom start 24 var linkValue = ""; 25 var isVariant = ""; 26 var priceCheck = ""; 27 if (product != null) 28 { 29 linkValue = product.ProductFields.Where(x => x.Key == "LinkValue").FirstOrDefault().Value.Value.ToString(); 30 isVariant = product.ProductFields.Where(x => x.Key == "IsVariant").FirstOrDefault().Value.Value.ToString(); 31 priceCheck = product.GetPrice().Price.ToString(); 32 } 33 //custom end 34 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", ""); 35 bool anonymousUser = Pageview.User == null; 36 bool isErpConnectionDown = !Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsWebServiceConnectionAvailable"]); 37 bool hidePrice = anonymousUsersLimitations.Contains("price") && anonymousUser || Pageview.AreaSettings.GetBoolean("ErpDownHidePrices") && isErpConnectionDown; 38 } 39 40 @if (product is object && !hidePrice && ((string.IsNullOrEmpty(linkValue)) || (!string.IsNullOrEmpty(linkValue) && Convert.ToBoolean(isVariant))) && !string.IsNullOrEmpty(priceCheck)) 41 { 42 bool showInformativePrice = Model.Item.GetBoolean("ShowInformativePrice"); 43 string unitId = product.DefaultUnitId; 44 45 string priceFontSize = Model.Item.GetRawValueString("PriceSize", "fs-2"); 46 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 47 string layout = Model.Item.GetRawValueString("Layout", "horizontal"); 48 string textAlign = horizontalAlign == "center" ? "text-center" : string.Empty; 49 textAlign = horizontalAlign == "end" ? "text-end" : textAlign; 50 51 horizontalAlign = horizontalAlign == "center" && layout == "horizontal" ? "justify-content-center" : horizontalAlign; 52 horizontalAlign = horizontalAlign == "end" && layout == "horizontal" ? "justify-content-end" : horizontalAlign; 53 horizontalAlign = horizontalAlign == "center" && layout == "vertical" ? "align-items-center" : horizontalAlign; 54 horizontalAlign = horizontalAlign == "end" && layout == "vertical" ? "align-items-end" : horizontalAlign; 55 56 string flexDirection = layout == "horizontal" ? string.Empty : "flex-column"; 57 string flexGap = layout == "horizontal" ? "gap-3" : string.Empty; 58 string order = layout == "horizontal" ? string.Empty : "order-2"; 59 string theme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("Theme")) ? "theme " + Model.Item.GetRawValueString("Theme").Replace(" ", "").Trim().ToLower() : ""; 60 theme = GetViewParameter("theme") != null ? GetViewParameterString("theme") : theme; 61 62 string contentPadding = Model.Item.GetRawValueString("ContentPadding", ""); 63 contentPadding = contentPadding == "none" ? "p-0" : contentPadding; 64 contentPadding = contentPadding == "small" ? "p-1 px-md-2 py-md-1" : contentPadding; 65 contentPadding = contentPadding == "large" ? "p-2 px-md-3 py-md-2" : contentPadding; 66 67 68 69 //custom edit start 70 string showPricesWithVat = Pageview.Area.EcomPricesWithVat.ToLower(); 71 //custom edit end 72 bool neverShowVat = string.IsNullOrEmpty(showPricesWithVat); 73 74 string priceMin = ""; 75 string priceMax = ""; 76 77 string liveInfoClass = ""; 78 string productInfoFeed = ""; 79 bool isLazyLoadingForProductInfoEnabled = Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsLazyLoadingForProductInfoEnabled"]); 80 if (isLazyLoadingForProductInfoEnabled) 81 { 82 if (Dynamicweb.Context.Current.Items.Contains("ProductInfoFeed")) 83 { 84 productInfoFeed = Dynamicweb.Context.Current.Items["ProductInfoFeed"]?.ToString(); 85 if (!string.IsNullOrEmpty(productInfoFeed)) 86 { 87 productInfoFeed = $"data-product-info-feed=\"{productInfoFeed}\""; 88 } 89 } 90 liveInfoClass = "js-live-info"; 91 } 92 93 <div class="@textAlign @liveInfoClass item_@Model.Item.SystemName.ToLower()" data-product-id="@product.Id" data-variant-id="@product.VariantId" @productInfoFeed> 94 @if (showInformativePrice && product.PriceInformative.Price != 0) 95 { 96 <div class="opacity-50"> 97 <span>@Translate("RRP") </span> 98 <span class="text-decoration-line-through text-price">@product.PriceInformative.PriceFormatted</span> 99 </div> 100 } 101 <div class="@priceFontSize m-0 d-flex @flexDirection @flexGap @horizontalAlign" itemprop="offers" itemscope itemtype="https://schema.org/Offer"> 102 <span itemprop="priceCurrency" content="@product.Price.CurrencyCode" class="d-none"></span> 103 104 105 @if (showPricesWithVat == "false" && !neverShowVat) 106 { 107 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 108 { 109 <span itemprop="price" content="" class="d-none"></span> 110 <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> 111 } 112 else 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 } 123 else 124 { 125 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 126 { 127 <span itemprop="price" content="" class="d-none"></span> 128 <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> 129 } 130 else 131 { 132 string beforePrice = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).PriceBeforeDiscount.PriceFormatted : product.PriceBeforeDiscount.PriceFormatted; 133 134 <span itemprop="price" content="@product.Price.Price" class="d-none"></span> 135 136 if (product.Price.Price != product.PriceBeforeDiscount.Price) 137 { 138 <span class="text-decoration-line-through opacity-75 @order"> 139 <span class="text-price">@beforePrice</span> 140 </span> 141 } 142 } 143 } 144 145 @if (showPricesWithVat == "false" && !neverShowVat) 146 { 147 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 148 { 149 <span class="text-price js-text-price"> 150 <span class="spinner-border" role="status"></span> 151 </span> 152 } 153 else 154 { 155 string price = product.GetPrice(unitId).Price.PriceWithoutVatFormatted; 156 157 // live price with default unit 158 if (!string.IsNullOrEmpty(product.DefaultUnitId)) 159 { 160 var dbProduct = Dynamicweb.Ecommerce.Services.Products.GetProductById(product.Id, product.VariantId, product.LanguageId); 161 var currency = Dynamicweb.Ecommerce.Services.Currencies.GetCurrency(Pageview.Area.EcomCurrencyId); 162 var country = Dynamicweb.Ecommerce.Services.Countries.GetCountry(Pageview.Area.EcomCountryCode); 163 164 Dynamicweb.Ecommerce.Prices.PriceContext priceContext = new Dynamicweb.Ecommerce.Prices.PriceContext(currency, country); 165 Settings settingsByShop = SettingsManager.GetSettingsByShop(Pageview.Area.EcomShopId); 166 LiveContext liveContext = new LiveContext(priceContext); 167 168 ProductInfo productInfo = ProductManager.GetProductInfo(dbProduct, settingsByShop, Pageview.User, liveContext, dbProduct.DefaultUnitId); 169 170 if (productInfo != null && productInfo.TryGetValue("Prices", out object pricesObj) && pricesObj is List<Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductPrice>) 171 { 172 var prices = pricesObj as List<Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductPrice>; 173 foreach (var livePrice in prices) 174 { 175 if (livePrice.UnitId == product.DefaultUnitId && livePrice.Amount.HasValue) 176 { 177 price = livePrice.Amount.GetValueOrDefault().ToString("N2") + " " + currency.Symbol; 178 } 179 } 180 } 181 } 182 if (product?.VariantInfo?.VariantInfo != null) 183 { 184 priceMin = product?.VariantInfo?.PriceMin?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithoutVatFormatted : ""; 185 priceMax = product?.VariantInfo?.PriceMax?.PriceWithoutVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithoutVatFormatted : ""; 186 } 187 if (priceMin != priceMax) 188 { 189 price = priceMin + " - " + priceMax; 190 } 191 192 <span class="@theme @contentPadding"> 193 <span class="text-price">@price</span> 194 </span> 195 } 196 } 197 else 198 { 199 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 200 { 201 <span class="text-price js-text-price"> 202 <span class="spinner-border" role="status"></span> 203 </span> 204 } 205 else 206 { 207 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceFormatted : product.Price.PriceFormatted; 208 209 if (product?.VariantInfo?.VariantInfo != null) 210 { 211 priceMin = product?.VariantInfo?.PriceMin?.PriceFormatted != null ? product.VariantInfo.PriceMin.PriceFormatted : ""; 212 priceMax = product?.VariantInfo?.PriceMax?.PriceFormatted != null ? product.VariantInfo.PriceMax.PriceFormatted : ""; 213 } 214 if (priceMin != priceMax) 215 { 216 price = priceMin + " - " + priceMax; 217 } 218 <span class="@theme @contentPadding"> 219 <span class="text-price">@price</span> 220 </span> 221 } 222 } 223 224 @* Stock state for Schema.org, start *@ 225 @{ 226 Uri url = Dynamicweb.Context.Current.Request.Url; 227 } 228 229 <link itemprop="url" href="@url"> 230 231 @{ 232 bool IsNeverOutOfStock = product.NeverOutOfstock; 233 } 234 235 @if (IsNeverOutOfStock) 236 { 237 <span itemprop="availability" class="d-none">@Translate("Available in stock")</span> 238 } 239 else 240 { 241 if (product.StockLevel > 0) 242 { 243 <span itemprop="availability" class="d-none">InStock</span> 244 } 245 else 246 { 247 <span itemprop="availability" class="d-none">OutOfStock</span> 248 } 249 } 250 @* Stock state for Schema.org, stop *@ 251 252 </div> 253 @*custom edit start*@ 254 @if (showPricesWithVat == "false" && !neverShowVat) 255 { 256 257 if (isLazyLoadingForProductInfoEnabled && !Pageview.IsVisualEditorMode) 258 { 259 260 @* Custom edit start *@ 261 @*<small class="opacity-85 fst-normal js-text-price-with-vat d-none" data-suffix="@Translate("Incl. VAT")"></small>*@ 262 @* Custom edit end *@ 263 } 264 else 265 { 266 267 string price = !string.IsNullOrEmpty(unitId) ? product.GetPrice(unitId).Price.PriceFormatted : product.Price.PriceFormatted; 268 269 270 if (product?.VariantInfo?.VariantInfo != null) 271 { 272 priceMin = product?.VariantInfo?.PriceMin?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMin.PriceWithVatFormatted : ""; 273 priceMax = product?.VariantInfo?.PriceMax?.PriceWithVatFormatted != null ? product.VariantInfo.PriceMax.PriceWithVatFormatted : ""; 274 } 275 if (priceMin != priceMax) 276 { 277 price = priceMin + " - " + priceMax; 278 } 279 @* Custom edit start *@ 280 @*<small class="opacity-85 fst-normal">@price @Translate("Incl. VAT")</small>*@ 281 @* Custom edit end *@ 282 } 283 } 284 @*custom edit end*@ 285 </div> 286 } 287 else if (Pageview.IsVisualEditorMode) 288 { 289 <div class="alert alert-dark m-0" role="alert"> 290 <span>@Translate("No products available")</span> 291 </div> 292 } 293