Product List Card forside
Error executing template "/Designs/Swift-v2/Paragraph/Custom_Swift-v2_ProductHeader.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_dcd488c3b5304105afc89d15891333ea.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 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 (!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 } 20 else if (Pageview.IsVisualEditorMode) 21 { 22 product = new ProductViewModel(); 23 product.Name = Translate("My product"); 24 product.Number = "12345"; 25 } 26 27 string manufacturer = null; 28 if (product?.ProductFields != null && product.ProductFields.TryGetValue("ManufacturerField", out var fv)) 29 { 30 manufacturer = fv?.Value?.ToString(); 31 } 32 if (string.IsNullOrWhiteSpace(manufacturer) && Pageview.IsVisualEditorMode) 33 { 34 manufacturer = Translate("Manufacturer"); 35 } 36 37 //Marked Products 38 string markedItem = string.Empty; 39 bool isPunchoutUser = false; 40 FieldValueViewModel specPrice; 41 bool hasSpecPrice = false; 42 43 if (!Pageview.IsVisualEditorMode && Pageview.User != null) 44 { 45 isPunchoutUser = Pageview.User.CustomFieldValues.SingleOrDefault(x => x.CustomField.SystemName == "AccessUser_IsPunchoutUser").Value.ToString().Equals("True"); 46 hasSpecPrice = product.ProductFields.TryGetValue("SpecPrice", out specPrice); 47 markedItem = specPrice != null && specPrice.Value.ToString() == "yes" && isPunchoutUser ? "markeditem" : ""; 48 } 49 50 string lastPurchaseDate = null; 51 if (product?.ProductFields != null && product.ProductFields.TryGetValue("LastPurchaseDate", out var fvDate)) 52 { 53 lastPurchaseDate = fvDate?.Value?.ToString(); 54 } 55 if (string.IsNullOrWhiteSpace(lastPurchaseDate) && Pageview.IsVisualEditorMode) 56 { 57 lastPurchaseDate = Translate("01-01-2025"); 58 } 59 60 string lastPurchaseQuantity = null; 61 if (product?.ProductFields != null && product.ProductFields.TryGetValue("LastPurchaseQuantity", out var fvQty)) 62 { 63 lastPurchaseQuantity = fvQty?.Value?.ToString(); 64 } 65 if (string.IsNullOrWhiteSpace(lastPurchaseQuantity) && Pageview.IsVisualEditorMode) 66 { 67 lastPurchaseQuantity = "10"; 68 } 69 70 string clickProductLink = ""; 71 72 if (Pageview.IsVisualEditorMode) 73 { 74 clickProductLink = ""; 75 } 76 else 77 { 78 clickProductLink = "onclick=\"return clickProductLink('" + @product.Id + "', '" + @product.Name + "', '" + @product.VariantName + "', '" + @product.Price.CurrencyCode + "', '" + @product.Price.ToStringInvariant() + "', '" + @product.Discount.ToStringInvariant() + "')\""; 79 } 80 string link = product.GetProductLink(GetPageIdByNavigationTag("Shop"), false); 81 if (!Pageview.IsVisualEditorMode) 82 { 83 string ReplacementProduct = !string.IsNullOrEmpty(product.ReplacementProduct.ProductId) ? product.ReplacementProduct.GetProduct().GetProductLink(GetPageIdByNavigationTag("Shop"), false) : ""; 84 link = !string.IsNullOrEmpty(ReplacementProduct) ? ReplacementProduct : link; 85 } 86 } 87 88 <div class="position-relative"> 89 90 <a href="@link" class="stretched-link" onmouseover="swift.Image.swapImage(event)" onmouseout="swift.Image.swapImage(event)" @clickProductLink> 91 <span class="visually-hidden">@product.Name</span> 92 </a> 93 94 <script> 95 function clickProductLink(productId, productName, productVariant, productCurrency, productPrice, productDiscount) { 96 if (typeof gtag !== "undefined") { 97 gtag("event", "select_item", { 98 item_list_id: "product_list_item_repeater", 99 item_list_name: "Product list (Item Repeater)", 100 items: [ 101 { 102 item_id: productId, 103 item_name: productName, 104 currency: productCurrency, 105 item_list_id: "product_list_item_repeater", 106 item_list_name: "Product list (Item Repeater)", 107 item_variant: productVariant, 108 price: productPrice, 109 discount: productDiscount 110 } 111 ] 112 }); 113 } 114 } 115 </script> 116 117 @if (product is object) 118 { 119 string titleFontSize = Model.Item.GetRawValueString("FontSize", "fs-6"); 120 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 121 horizontalAlign = horizontalAlign == "center" ? "text-center" : horizontalAlign; 122 horizontalAlign = horizontalAlign == "end" ? "text-end" : horizontalAlign; 123 124 string headingLevel = Model.Item.GetString("HeadingLevel", "h2"); 125 string headingLevelStart = $"<{headingLevel} class=\"{titleFontSize} {horizontalAlign} {markedItem} fw-bold m-0 item_{Model.Item.SystemName.ToLower()}\" itemprop=\"name\">"; 126 string headingLevelStop = $"</{headingLevel}>"; 127 128 @headingLevelStart 129 @product.Name 130 @headingLevelStop 131 132 <span class="fs-7 opacity-85 @horizontalAlign fw-bold"> 133 @product.Number 134 @if (!string.IsNullOrWhiteSpace(manufacturer)) 135 { 136 @($" Producent: {manufacturer}") 137 } 138 </span> 139 140 @if (!string.IsNullOrWhiteSpace(lastPurchaseDate) || !string.IsNullOrWhiteSpace(lastPurchaseQuantity)) 141 { 142 <span class="fs-7 opacity-85 @horizontalAlign d-block fw-bold"> 143 @Translate("Last purchase"): @lastPurchaseDate, @Translate("Quantity"): @lastPurchaseQuantity 144 </span> 145 } 146 } 147 </div> 148