Error executing template "Designs/Swift/Paragraph/Swift_ProductAddToCart.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
at CompiledRazorTemplates.Dynamic.RazorEngine_c1e33387410f4cf6935dc446f66f12c9.Execute() in D:\dynamicweb.net\Solutions\Degree\proff1.cloud.dynamicweb-cms.com\files\Templates\Designs\Swift\Paragraph\Swift_ProductAddToCart.cshtml:line 41
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 Proff1.Service
5 @using System.Text.Json
6
7
8 @{
9 ProductViewModel product = null;
10 List<Dynamicweb.Ecommerce.ProductCatalog.GroupInfoViewModel> productGroup = null;
11 string lastGroup = null;
12 string productIdTag = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("ProductID")) ? Dynamicweb.Context.Current.Request.QueryString.Get("ProductID") : "";
13 var productService = new Dynamicweb.Ecommerce.Products.ProductService();
14 var regularProduct = productService.GetProductById(productIdTag, "", true);
15 var productBrand = regularProduct?.ProductFieldValues.GetProductFieldValue("TK_Brand").Value.ToString();
16
17
18 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
19 {
20 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
21 productGroup = product.GroupPaths[0];
22 lastGroup = productGroup.Last().Name;
23 }
24 else if (Pageview.Item["DummyProduct"] != null)
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 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", "");
36 bool anonymousUser = Pageview.User == null;
37 bool isErpConnectionDown = !Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsWebServiceConnectionAvailable"]);
38 bool hideAddToCart = anonymousUsersLimitations.Contains("cart") && anonymousUser || Pageview.AreaSettings.GetBoolean("ErpDownHideAddToCart") && isErpConnectionDown;
39 hideAddToCart = Pageview.IsVisualEditorMode ? false : hideAddToCart;
40
41 string addedMessageId = $"addedMsg{product.Id}_{Pageview.CurrentParagraph.ID}";
42 string addToCartButtonId = $"AddToCartButton{product.Id}_{Pageview.CurrentParagraph.ID}";
43 }
44
45
46 @if (product is object && !hideAddToCart)
47 {
48 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", "");
49 horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign;
50 horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign;
51 horizontalAlign = horizontalAlign == "full" ? "" : horizontalAlign;
52
53 bool favoritesSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowAddToFavorites")) ? Model.Item.GetBoolean("ShowAddToFavorites") : false;
54 bool quantitySelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowQuantitySelector")) ? Model.Item.GetBoolean("ShowQuantitySelector") : false;
55 bool unitsSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowUnitsSelector")) ? Model.Item.GetBoolean("ShowUnitsSelector") : false;
56 bool hideInventory = !string.IsNullOrEmpty(Model.Item.GetString("HideInventory")) ? Model.Item.GetBoolean("HideInventory") : false;
57 bool hideStockState = !string.IsNullOrEmpty(Model.Item.GetString("HideStockState")) ? Model.Item.GetBoolean("HideStockState") : false;
58
59 string buttonSize = Model.Item.GetRawValueString("ButtonSize", "regular");
60 string inputSize = string.Empty;
61
62 switch (buttonSize)
63 {
64 case "small":
65 inputSize = " input-group-sm";
66 buttonSize = " btn-sm";
67 break;
68 case "regular":
69 buttonSize = string.Empty;
70 break;
71 case "large":
72 inputSize = " input-group-lg";
73 buttonSize = " btn-lg";
74 break;
75 }
76
77 string iconPath = "/Files/icons/";
78 string url = "/Default.aspx?ID=" + (GetPageIdByNavigationTag("CartService"));
79 if (!url.Contains("LayoutTemplate"))
80 {
81 url += url.Contains("?") ? "&LayoutTemplate=Swift_MiniCart.cshtml" : "?LayoutTemplate=Swift_MiniCart.cshtml";
82 }
83
84 string disableAddToCart = (product.StockLevel <= 0) ? "disabled" : "";
85 bool isNeverOutOfStock = product.NeverOutOfstock;
86 disableAddToCart = isNeverOutOfStock ? "" : disableAddToCart;
87
88 string whenVariantsExist = Model.Item.GetRawValueString("WhenVariantsExist", "hide");
89
90 string flexFill = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "flex-fill" : "";
91 string fullWidth = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "w-100" : "";
92 string addToCartIcon = Model.Item.GetRawValueString("Icon", iconPath + "shopping-cart.svg");
93 string checkIcon = Model.Item.GetRawValueString("Icon", iconPath + "check.svg");
94 string addToCartLabel = !addToCartIcon.Contains("_none") ? "<span class=\"icon-2\">" + ReadFile(addToCartIcon) + "</span>" : "";
95 addToCartLabel += !addToCartIcon.Contains("_none") && !Model.Item.GetBoolean("HideButtonText") ? " " : "";
96 addToCartLabel += !Model.Item.GetBoolean("HideButtonText") ? Translate("Add to cart") : "";
97 string productId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("ProductID")) ? Dynamicweb.Context.Current.Request.QueryString.Get("ProductID") : "";
98 bool isProductDetailsPage = !string.IsNullOrEmpty(productId);
99
100 //if (product.VariantInfo.VariantInfo == null || whenVariantsExist == "disable") {
101
102 if (product.VariantInfo.VariantInfo == null || isProductDetailsPage)
103 {
104 string unitId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("UnitId")) ? Dynamicweb.Context.Current.Request.Form.Get("UnitId") : product.DefaultUnitId;
105 if (string.IsNullOrEmpty(unitId) && product?.UnitOptions != null)
106 {
107 if (product.UnitOptions.FirstOrDefault<UnitOptionViewModel>() != null)
108 {
109 unitId = product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Id;
110 }
111 }
112
113 string minQty = product.PurchaseMinimumQuantity != 1 ? "min=\"" + product.PurchaseMinimumQuantity.ToString() + "\"" : "min=\"1\"";
114 string stepQty = product.PurchaseQuantityStep > 1 ? product.PurchaseQuantityStep.ToString() : "1";
115 string valueQty = product.PurchaseMinimumQuantity > product.PurchaseQuantityStep ? product.PurchaseMinimumQuantity.ToString() : stepQty;
116 disableAddToCart = product.VariantInfo.VariantInfo != null && string.IsNullOrEmpty(product.VariantId) ? "disabled" : disableAddToCart;
117
118 var reserveMode = Dynamicweb.Ecommerce.Frontend.Cart.ProductReserve.Mode;
119
120 if (unitsSelector && product.UnitOptions.Count > 0)
121 {
122 <form method="post" action="/Default.aspx?ID=@(Pageview.Page.ID)&ProductId=@product.Id" id="UnitSelectorForm_@(product.Id)_@(product.VariantId)_@Model.ID">
123 <input type="hidden" name="redirect" value="false">
124 <input type="hidden" name="VariantID" value="@product.VariantId">
125 <input type="hidden" name="UnitID" class="js-unit-id" value="@unitId">
126 </form>
127 }
128
129 <div class="d-grid @horizontalAlign @fullWidth js-input-group item_@Model.Item.SystemName.ToLower()">
130 <form method="post" action="@url" class="@fullWidth" style="z-index: 1">
131 <input type="hidden" name="redirect" value="false">
132 <input type="hidden" name="ProductId" value="@product.Id">
133 <input type="hidden" name="ProductName" value="@product.Name">
134 <input type="hidden" name="ProductVariantName" value="@product.VariantName">
135 <input type="hidden" name="ProductCurrency" value="@Dynamicweb.Ecommerce.Common.Context.Currency.Code">
136 <input type="hidden" name="ProductPrice" value="@PriceViewModelExtensions.ToStringInvariant(product.Price)">
137 <input type="hidden" name="ProductReferer" value="component_ProductAddToCart">
138 <input type="hidden" name="cartcmd" value="add">
139
140 @if (reserveMode == Dynamicweb.Ecommerce.Frontend.Cart.ProductReserveMode.AddToCart)
141 {
142 <input type="hidden" name="GetReservedAmount" value="true">
143 }
144
145 @if (!string.IsNullOrEmpty(product.VariantId))
146 {
147 <input type="hidden" name="VariantId" value="@product.VariantId">
148 }
149
150 @if (!product.NeverOutOfstock)
151 {
152 <input type="hidden" name="Stock" value="@product.StockLevel">
153
154 <template class="js-out-of-stock-notice">
155 <div class="modal-header">
156 <h1 class="modal-title fs-5">@Translate("Stock limit")</h1>
157 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
158 </div>
159 <div class="modal-body">
160 @Translate("There are not enough products in stock. The product might be sold out or discontinued. Please adjust the quantity.")
161 </div>
162 </template>
163 }
164
165 @if (stepQty != "1")
166 {
167 <template class="js-step-quantity-warning">
168 <div class="modal-header">
169 <h1 class="modal-title fs-5">@Translate("The quantity is not valid")</h1>
170 </div>
171 <div class="modal-body">
172 @Translate("Please select a quantity that is dividable by") @stepQty
173 </div>
174 </template>
175 }
176 @if (product.PurchaseMinimumQuantity != 1)
177 {
178 <template class="js-min-quantity-warning">
179 <div class="modal-header">
180 <h1 class="modal-title fs-5">@Translate("The product could not be added to the cart")</h1>
181 </div>
182 <div class="modal-body">
183 @Translate("The quantity is not valid. You must buy at least") @product.PurchaseMinimumQuantity
184 </div>
185 </template>
186 }
187
188 @if (quantitySelector || (!anonymousUser && product.VariantInfo.VariantInfo != null) || (!anonymousUser && favoritesSelector))
189 {
190 <input type="hidden" id="Unit_@(product.Id)_@product.VariantId" name="UnitID" value="@unitId" />
191 }
192
193 <div class="d-flex flex-row w-100">
194 @if (!anonymousUser && favoritesSelector)
195 {
196 @RenderPartial("Components/ToggleFavorite.cshtml", product)
197 }
198
199 @if (!quantitySelector)
200 {
201 <input id="Quantity_@(product.Id)_@product.VariantId" name="Quantity" value="@valueQty" type="hidden" @disableAddToCart>
202 }
203
204 <div class="input-group input-primary-button-group flex-nowrap@(inputSize)">
205 @if (quantitySelector)
206 {
207 <div class="d-flex me-2" style="border: 1px solid #5555">
208 <button type="button" class="btn btn-stepper fs-6 fw-normal rounded-0" onclick="decreaseQuantity('Quantity_@(product.Id)_@product.VariantId')" style="border-right: 1px solid #5555">-</button>
209 <input id="Quantity_@(product.Id)_@product.VariantId" name="Quantity" value="@valueQty" step="@stepQty" @minQty class="form-control swift_quantity-field text-center border-white" style="max-width: 20px; z-index: 1" type="number" onchange="swift.Cart.UpdateOnEnterKey(event)" onkeyup="swift.Cart.UpdateOnEnterKey(event)" @disableAddToCart>
210 <button type="button" class="btn btn-stepper fs-6 fw-normal m-r-5 rounded-0" onclick="increaseQuantity('Quantity_@(product.Id)_@product.VariantId')" style="border-left: 1px solid #5555">+</button>
211 </div>
212 }
213
214 @if (unitsSelector && product.UnitOptions.Count > 0)
215 {
216 string selectedUnitName = !string.IsNullOrEmpty(unitId) && product?.UnitOptions != null ? unitId : product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Name;
217
218 foreach (var unitOption in product.UnitOptions)
219 {
220 if (unitOption.Id == unitId)
221 {
222 selectedUnitName = unitOption.Name;
223 }
224 }
225
226 <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
227 @selectedUnitName
228 </button>
229 <ul class="dropdown-menu swift_unit-field">
230 @foreach (var unitOption in product.UnitOptions)
231 {
232 var selectedUnit = unitOption.Id == unitId ? "selected" : "";
233
234 <li>
235 <button type="button" class="btn dropdown-item" data-value="@unitOption.Id" onclick="document.querySelector('#UnitSelectorForm_@(product.Id)_@(product.VariantId)_@Model.ID').querySelector('.js-unit-id').value = this.getAttribute('data-value');
236 document.querySelector('#Unit_@(product.Id)_@product.VariantId').value = this.getAttribute('data-value');
237 swift.PageUpdater.Update(document.querySelector('#UnitSelectorForm_@(product.Id)_@(product.VariantId)_@Model.ID'))">
238 <span>@unitOption.Name</span>
239 <span>
240 @if (unitOption.StockLevel > 0)
241 {
242 if (!Model.Item.GetBoolean("HideInventory"))
243 {
244 <span class="small text-success">@unitOption.StockLevel @Translate("In stock")</span>
245 }
246 else
247 {
248 <span class="small text-success">@Translate("In stock")</span>
249 }
250 }
251 else
252 {
253 <span class="small text-danger">@Translate("Out of Stock")</span>
254 }
255 </span>
256 </button>
257 </li>
258 }
259 </ul>
260 }
261
262 <div>
263 <button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary @(buttonSize) @flexFill js-add-to-cart-button rounded " style="white-space: nowrap" @disableAddToCart title="@Translate("Add to cart")" id="@addToCartButtonId">
264 @if (!Model.Item.GetBoolean("HideButtonText"))
265 {
266 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2">
267 @addToCartLabel
268 </span>
269
270 }
271 else
272 {
273 @addToCartLabel
274 }
275 </button>
276
277 @if (isProductDetailsPage)
278 {
279
280 <p class="position-absolute top-100 mt-2 pb-2 d-none" id="addedMsg@(product.Id)_@Pageview.CurrentParagraph.ID">@Translate("Lagt i handlekurv")</p>
281 }
282 else
283 {
284 <p class="position-absolute top-100 end-0 mt-2 pb-2 d-none" id="addedMsg@(product.Id)_@Pageview.CurrentParagraph.ID">@Translate("Lagt i handlekurv")</p>
285
286 }
287 </div>
288
289
290 </div>
291 </div>
292 </form>
293
294 @if (isProductDetailsPage)
295 {
296 <div class="stock-status py-2">
297 <span class="stock-label text-decoration-underline">Lagerstatus</span>
298
299 <div class="stock-info">
300 @foreach (var stockInfo in Proff1Services.StockService.GetStockInfo(product.Number))
301 {
302
303 <span>@stockInfo.LocationName: @stockInfo.ItemsInStock</span><br />
304 @*<span>Tonsberg: 0</span>
305 <span>Larvik: 0</span>*@
306
307 }
308 </div>
309 </div>
310 }
311 </div>
312
313 }// else if (whenVariantsExist == "modal") {
314 else
315 {
316 string buttonText = Translate("Select");
317
318 string variantSelectorServicePageId = !string.IsNullOrEmpty(Model.Item.GetString("VariantSelectorServicePageId")) ? Model.Item.GetLink("VariantSelectorServicePageId").PageId.ToString() : "";
319 variantSelectorServicePageId = variantSelectorServicePageId != "" ? variantSelectorServicePageId : GetPageIdByNavigationTag("VariantSelectorService").ToString();
320 string link = product.GetProductLink(GetPageIdByNavigationTag("Shop"), false);
321
322 <div class="d-flex @horizontalAlign w-100 item_@Model.Item.SystemName.ToLower()">
323 @if (!anonymousUser && favoritesSelector)
324 {
325 @RenderPartial("Components/ToggleFavorite.cshtml", product)
326 }
327 <form action="/Default.aspx?ID=@variantSelectorServicePageId" data-response-target-element="DynamicModalContent" data-preloader="inline" style="z-index: 1" class="@fullWidth">
328 <input type="hidden" name="ProductID" value="@product.Id">
329 <input type="hidden" name="QuantitySelector" value="@quantitySelector.ToString()">
330 <input type="hidden" name="HideInventory" value="@hideInventory.ToString()">
331 <input type="hidden" name="HideStockState" value="@hideStockState.ToString()">
332 <input type="hidden" name="VariantSelectorServicePage" value="@variantSelectorServicePageId">
333 <input type="hidden" name="ViewType" value="ModalContent">
334 <a href="@link" class="btn btn-primary" title="@Translate("Select")">@buttonText</a>
335 </form>
336 </div>
337 }
338 }
339 else if (Pageview.IsVisualEditorMode)
340 {
341 <div class="alert alert-dark m-0">@Translate("No products available")</div>
342 }
343
344 @{
345 var text = "available online and in store.";
346 var translated = @Translate(text);
347 string metaDescription = $"Vi har {(string.IsNullOrEmpty(productBrand) ? "" : productBrand + " ")}{product.Name} {translated} Proff1 hjelper deg med {lastGroup}.";
348 string metaTitle = $"{(string.IsNullOrEmpty(productBrand) ? "" : productBrand + " ")}{product.Name}";
349 }
350
351 <script>
352
353 function decreaseQuantity(inputId) {
354 const input = document.getElementById(inputId);
355 const currentValue = parseInt(input.value);
356
357 if (currentValue > 1) {
358 input.value = currentValue - 1;
359 }
360 }
361
362 function increaseQuantity(inputId) {
363 const input = document.getElementById(inputId);
364 const currentValue = parseInt(input.value);
365
366 input.value = currentValue + 1;
367 }
368
369 function UpdateCart(productId) {
370 console.log("Product ID:", productId);
371
372
373 }
374
375 function UpdateCart(event) {
376 var buttonId = event.target.id;
377
378 var productId = buttonId.split('_')[0].replace('AddToCartButton', '');
379
380
381 var messageElement = document.getElementById("@addedMessageId");
382
383
384 messageElement.classList.remove('d-none');
385
386 setTimeout(function () {
387 messageElement.classList.add('d-none');
388 }, 1000); // 3000 milliseconds = 3 seconds
389 };
390
391
392 var addToCartButton = document.getElementById("@addToCartButtonId");
393
394 if (addToCartButton) {
395
396 addToCartButton.addEventListener('click', UpdateCart);
397 }
398 </script>
399
400
401 @if (!string.IsNullOrEmpty(productIdTag))
402 {
403 <!--$$SnippetStart(meta)-->
404 <meta name="description" content="@metaDescription" />
405 <title>@metaTitle</title>
406 <meta property="og:title" content="@metaTitle">
407
408 <!--$$SnippetEnd(meta)-->
409 }
410