Error executing template "Designs/Swift/Paragraph/Swift_ProductAddToFavorites_Custom.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
at CompiledRazorTemplates.Dynamic.RazorEngine_383dd0440a7f4dc386e84e2c11c12e80.Execute() in D:\dynamicweb.net\Solutions\Dynamicweb\bronsmaskinforretning.cloud.dynamicweb-cms.com\files\Templates\Designs\Swift\Paragraph\Swift_ProductAddToFavorites_Custom.cshtml:line 100
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 Dynamicweb.Ecommerce.CustomerExperienceCenter.Favorites
4 @using System.Globalization
5
6
7 @functions {
8 string DoubleToString(double? value)
9 {
10 if (value.HasValue)
11 {
12 return value.Value.ToString(CultureInfo.InvariantCulture);
13 }
14 return null;
15 }
16 }
17
18 @{
19 ProductViewModel product = null;
20 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
21 {
22 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
23 }
24 else if (Pageview.Page.Item["DummyProduct"] != null && Pageview.IsVisualEditorMode)
25 {
26 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page);
27 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel();
28
29 if (productList?.Products is object)
30 {
31 product = productList.Products[0];
32 }
33 }
34
35 bool anonymousUser = Pageview.User == null;
36
37 bool productIsDiscontinued = product is object && product.Discontinued;
38 bool disableAddToCartIfProductIsDiscontinued = Model.Item.GetBoolean("DoNotShowFavoriteButtonIfProductIsDiscontinued");
39 bool isDiscontinued = productIsDiscontinued && disableAddToCartIfProductIsDiscontinued;
40
41 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", string.Empty);
42 horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign;
43 horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign;
44 horizontalAlign = horizontalAlign == "full" ? string.Empty : horizontalAlign;
45
46 string buttonSize = Model.Item.GetRawValueString("ButtonSize", "regular");
47 buttonSize = buttonSize == "small" ? " btn-sm" : buttonSize;
48 buttonSize = buttonSize == "regular" ? string.Empty : buttonSize;
49 buttonSize = buttonSize == "large" ? " btn-lg" : buttonSize;
50
51 string buttonStyle = Model.Item.GetRawValueString("ButtonStyle", "secondary");
52 string buttonStyleClass = string.Empty;
53
54 switch (buttonStyle)
55 {
56 case "primary":
57 buttonStyleClass = " btn-primary";
58 break;
59 case "secondary":
60 buttonStyleClass = " btn-secondary";
61 break;
62 case "link":
63 buttonStyleClass = " btn-link";
64 break;
65 }
66
67 string url = "/Default.aspx?ID=" + (GetPageIdByNavigationTag("CartService"));
68 if (!url.Contains("LayoutTemplate"))
69 {
70 url += url.Contains("?") ? "&LayoutTemplate=Swift_MiniCart.cshtml" : "?LayoutTemplate=Swift_MiniCart.cshtml";
71 }
72
73 string fullWidth = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "w-100" : "";
74
75 var favoriteParameters = new Dictionary<string, object>();
76 int defaultFavoriteListId = 0;
77
78 if (Pageview?.User?.ID != null)
79 {
80 IEnumerable<FavoriteList> favoreiteLists = Pageview.User.GetFavoriteLists();
81 if (favoreiteLists.Count() == 1)
82 {
83 foreach (FavoriteList list in favoreiteLists)
84 {
85 defaultFavoriteListId = list.ListId;
86 }
87 }
88 }
89
90 favoriteParameters.Add("ListId", defaultFavoriteListId);
91 favoriteParameters.Add("ButtonSize", buttonSize);
92 favoriteParameters.Add("ButtonStyle", buttonStyleClass);
93 favoriteParameters.Add("ButtonWidth", fullWidth);
94
95 if (!Model.Item.GetBoolean("HideButtonText"))
96 {
97 favoriteParameters.Add("ShowButtonText", true);
98 }
99
100 double? stepQty = product.PurchaseQuantityStep > 0 ? product.PurchaseQuantityStep : 1;
101 double? minQty = product.PurchaseMinimumQuantity > 0 ? product.PurchaseMinimumQuantity : 1;
102 double? valueQty = minQty > stepQty ? minQty : stepQty;
103 double? maxQty = null;
104 if (product.ProductType == Dynamicweb.Ecommerce.Products.ProductType.Stock && !product.NeverOutOfstock)
105 {
106 maxQty = product.StockLevel;
107 }
108
109 }
110
111 @if ((product is object && !anonymousUser && !isDiscontinued) || (product is object && Pageview.IsVisualEditorMode))
112 {
113 <div class="d-flex @horizontalAlign @fullWidth item_@Model.Item.SystemName.ToLower()">
114 <input class="form-control d-inline-block swift_quantity-field" style="min-width: 60px; max-width: 100px; z-index: 1" type="number" id="Quantity_@product.Id" name="Quantity" value="@DoubleToString(valueQty)" step="@DoubleToString(stepQty)" min="@DoubleToString(minQty)" max="@DoubleToString(maxQty)">
115 @RenderPartial("Components/ToggleFavorite_Custom.cshtml", product, favoriteParameters)
116 </div>
117 }
118
119 else if (Pageview.IsVisualEditorMode)
120 {
121 <div class="alert alert-dark m-0">@Translate("No products available")</div>
122 }
123