Komponent editor til: Add To cart

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_57e5d9f6dcae43d29a5468e16da96a97.Execute() in D:\dynamicweb.net\Solutions\RelateIT\fineman.cloud.dynamicweb-cms.com\files\Templates\Designs\Swift\Paragraph\Swift_ProductAddToCart.cshtml:line 28
   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.Core.Encoders 4 5 6 @{ 7 ProductViewModel product = null; 8 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 9 { 10 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 11 } 12 else if (Pageview.Page.Item["DummyProduct"] != null && Pageview.IsVisualEditorMode) 13 { 14 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page); 15 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel(); 16 17 if (productList?.Products is object) 18 { 19 product = productList.Products[0]; 20 } 21 } 22 23 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", ""); 24 bool anonymousUser = Pageview.User == null; 25 bool isErpConnectionDown = !Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsWebServiceConnectionAvailable"]); 26 bool hideAddToCart = anonymousUsersLimitations.Contains("cart") && anonymousUser || Pageview.AreaSettings.GetBoolean("ErpDownHideAddToCart") && isErpConnectionDown; 27 hideAddToCart = Pageview.IsVisualEditorMode ? false : hideAddToCart; 28 var hidePriceProductField = product.ProductFields.FirstOrDefault(f => f.Key == "ProductDoNotShowPrice"); 29 var doNotShowPrice = hidePriceProductField.Value?.ToString().ToLower() == "true"; 30 if(doNotShowPrice) 31 { 32 hideAddToCart = Pageview.IsVisualEditorMode ? false : true; 33 } 34 } 35 36 @if (product is object && !hideAddToCart) { 37 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 38 horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign; 39 horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign; 40 horizontalAlign = horizontalAlign == "full" ? "" : horizontalAlign; 41 42 bool favoritesSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowAddToFavorites")) ? Model.Item.GetBoolean("ShowAddToFavorites") : false; 43 bool quantitySelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowQuantitySelector")) ? Model.Item.GetBoolean("ShowQuantitySelector") : false; 44 bool unitsSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowUnitsSelector")) ? Model.Item.GetBoolean("ShowUnitsSelector") : false; 45 bool hideInventory = !string.IsNullOrEmpty(Model.Item.GetString("HideInventory")) ? Model.Item.GetBoolean("HideInventory") : false; 46 bool hideStockState = !string.IsNullOrEmpty(Model.Item.GetString("HideStockState")) ? Model.Item.GetBoolean("HideStockState") : false; 47 48 string buttonSize = Model.Item.GetRawValueString("ButtonSize", "regular"); 49 string inputSize = string.Empty; 50 51 switch (buttonSize) 52 { 53 case "small": 54 inputSize = " input-group-sm"; 55 buttonSize = " btn-sm"; 56 break; 57 case "regular": 58 buttonSize = string.Empty; 59 break; 60 case "large": 61 inputSize = " input-group-lg"; 62 buttonSize = " btn-lg"; 63 break; 64 } 65 66 string iconPath = "/Files/icons/"; 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 bool isLazyLoadingForProductInfoEnabled = Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsLazyLoadingForProductInfoEnabled"]); 74 string disableAddToCart = (product.StockLevel <= 0) ? "disabled" : ""; 75 bool isNeverOutOfStock = product.NeverOutOfstock; 76 disableAddToCart = isNeverOutOfStock && !isLazyLoadingForProductInfoEnabled ? "" : disableAddToCart; 77 78 string whenVariantsExist = Model.Item.GetRawValueString("WhenVariantsExist", "hide"); 79 80 string flexFill = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "flex-fill" : ""; 81 string fullWidth = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "w-100" : ""; 82 string addToCartIcon = Model.Item.GetRawValueString("Icon", iconPath + "shopping-cart.svg"); 83 string addToCartLabel = !addToCartIcon.Contains("_none") ? $"<span class=\"icon-2\">{ReadFile(addToCartIcon)}</span>" : ""; 84 addToCartLabel += !addToCartIcon.Contains("_none") && !Model.Item.GetBoolean("HideButtonText") ? " " : ""; 85 addToCartLabel += !Model.Item.GetBoolean("HideButtonText") ? $"<span class=\"d-none d-md-inline\">{Translate("Add to cart")}</span><span class=\"d-inline d-md-none\">{Translate("Add")}</span>" : ""; 86 87 bool userHasPendingQuote = Dynamicweb.Ecommerce.Common.Context.Cart != null && Dynamicweb.Ecommerce.Common.Context.Cart.IsQuote; 88 var productContactSales = product.ProductFields.FirstOrDefault(f => f.Key == "ProductContactSales"); 89 bool displayContactSales = productContactSales.Value?.ToString() == "True" ? true : false; 90 string dummyText = "Not implemented yet"; 91 92 // Fetch data from user 93 var currentUser = Pageview.User; 94 if(displayContactSales) 95 { 96 97 <div class="input-group"> 98 <a href="mailto:salg@fineman.dk?subject=Angående bestillingsvare: @product.Id / @(product.Name)&body=Hej Fineman Salg%0D%0A%0D%0AJeg vil gerne kontaktes angående varenummer @(product.Id) / @(product.Name).%0D%0A%0D%0AKundeoplysninger:%0D%0A Kundenavn: @currentUser.Name%0D%0A Adresse: @currentUser.Address%0D%0A By: @currentUser.City%0D%0A Postnummer: @currentUser.Zip%0D%0A Firma: @currentUser.Company:%0D%0A E-mail: @currentUser.Email%0D%0A" title="@Translate("Kontakt salg, produktet er en bestillingsvare")" id="ContactSalesButton@(product.Id)_@Pageview.CurrentParagraph.ID" class="btn btn-secondary btn-sm @flexFill">@Translate("Kontakt salg")</a> 99 </div> 100 } 101 else 102 { 103 if (product.VariantInfo.VariantInfo == null || whenVariantsExist == "disable") { 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 if (product.UnitOptions.FirstOrDefault<UnitOptionViewModel>() != null) { 107 unitId = product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Id; 108 } 109 } 110 111 double? stepQty = product.PurchaseQuantityStep > 0 ? product.PurchaseQuantityStep : 1; 112 double? minQty = product.PurchaseMinimumQuantity > 0 ? product.PurchaseMinimumQuantity : 1; 113 double? valueQty = minQty > stepQty ? minQty : stepQty; 114 bool isStockType = product.ProductType == Dynamicweb.Ecommerce.Products.ProductType.Stock; 115 bool neverOutOfStock = product.NeverOutOfstock; 116 bool setMaxQty = isStockType && !neverOutOfStock; 117 double? productStock = double.Parse(product.ProductFields.FirstOrDefault(f => f.Key == "ProductCustomStockLevel").Value.ToString() ?? "0"); 118 double? maxQty = setMaxQty ? productStock : null; 119 120 if (unitsSelector && product.UnitOptions.Count > 0) { 121 <form method="post" action="/Default.aspx?ID=@(Pageview.Page.ID)&ProductId=@product.Id" id="UnitSelectorForm_@(product.Id)_@(product.VariantId.Replace(".", "_"))_@Model.ID"> 122 <input type="hidden" name="redirect" value="false"> 123 <input type="hidden" name="VariantID" value="@product.VariantId"> 124 <input type="hidden" name="UnitID" class="js-unit-id" value="@unitId"> 125 </form> 126 } 127 128 <div class="d-flex @horizontalAlign @fullWidth js-input-group item_@Model.Item.SystemName.ToLower()"> 129 <form method="post" action="@url" class="@fullWidth" style="z-index: 1"> 130 <input type="hidden" name="redirect" value="false"> 131 <input type="hidden" name="ProductId" value="@product.Id"> 132 <input type="hidden" name="ProductName" value="@HtmlEncoder.HtmlEncode(product.Name)"> 133 <input type="hidden" name="ProductVariantName" value="@product.VariantName"> 134 <input type="hidden" name="ProductCurrency" value="@Dynamicweb.Ecommerce.Common.Context.Currency.Code"> 135 <input type="hidden" name="ProductPrice" value="@PriceViewModelExtensions.ToStringInvariant(product.Price)"> 136 <input type="hidden" name="ProductReferer" value="component_ProductAddToCart"> 137 <input type="hidden" name="cartcmd" value="add"> 138 <input type="submit" class="d-none" onclick="event.preventDefault(); swift.Cart.Update(event)"> @* Fix for enterKey should not redirect to minicart page *@ 139 140 @if (!string.IsNullOrEmpty(product.VariantId)) 141 { 142 <input type="hidden" name="VariantId" value="@product.VariantId"> 143 } 144 145 <template class="js-step-quantity-warning"> 146 <div class="modal-header"> 147 <h1 class="modal-title fs-5">@Translate("The quantity is not valid")</h1> 148 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> 149 </div> 150 <div class="modal-body"> 151 @Translate("Please select a quantity that is dividable by") @stepQty 152 </div> 153 </template> 154 155 156 <template class="js-min-quantity-warning"> 157 <div class="modal-header"> 158 <h1 class="modal-title fs-5">@Translate("The product could not be added to the cart")</h1> 159 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> 160 </div> 161 <div class="modal-body"> 162 @Translate("The quantity is not valid. You must buy at least") @product.PurchaseMinimumQuantity 163 </div> 164 </template> 165 166 167 @if (userHasPendingQuote) 168 { 169 <input type="hidden" name="PendingQuote" value="true"> 170 171 <template class="js-pending-quote-notice"> 172 <div class="modal-header"> 173 <h1 class="modal-title fs-5">@Translate("Pending Quote")</h1> 174 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="@Translate("Close")"></button> 175 </div> 176 <div class="modal-body"> 177 @Translate("You need to complete your current quote or empty the cart before adding this product to cart.") 178 </div> 179 </template> 180 } 181 182 @if (quantitySelector || (!anonymousUser && product.VariantInfo.VariantInfo != null) || (!anonymousUser && favoritesSelector)) 183 { 184 <input type="hidden" id="Unit_@(product.Id)_@product.VariantId.Replace(".", "_")" name="UnitID" value="@unitId" /> 185 } 186 187 <div class="d-flex flex-row w-100"> 188 @if (!quantitySelector) 189 { 190 <input id="Quantity_@(product.Id)_@product.VariantId.Replace(".", "_")" class="swift_quantity_field" name="Quantity" value="@valueQty" type="hidden" @disableAddToCart> 191 } 192 193 @if (unitsSelector && product.UnitOptions.Count > 0) 194 { 195 string selectedUnitName = !string.IsNullOrEmpty(unitId) && product?.UnitOptions != null ? unitId : product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Name; 196 197 foreach (var unitOption in product.UnitOptions) 198 { 199 if (unitOption.Id == unitId) 200 { 201 selectedUnitName = unitOption.Name; 202 } 203 } 204 205 <div class="d-flex flex-column gap-2 w-100"> 206 <div class="input-group input-primary-button-group flex-nowrap@(inputSize)"> 207 @if (!anonymousUser && favoritesSelector) 208 { 209 @RenderPartial("Components/ToggleFavorite.cshtml", product) 210 } 211 212 @if (quantitySelector) 213 { 214 <input id="Quantity_@(product.Id)_@product.VariantId.Replace(".", "_")" name="Quantity" value="@valueQty" step="@stepQty" min="@minQty" max="@maxQty" class="form-control swift_quantity-field" style="min-width: 60px; max-width: 100px; z-index: 1" type="number" @disableAddToCart> 215 } 216 217 <button class="btn btn-secondary @flexFill dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> 218 @selectedUnitName 219 </button> 220 221 <ul class="dropdown-menu swift_unit-field"> 222 @foreach (var unitOption in product.UnitOptions) 223 { 224 var selectedUnit = unitOption.Id == unitId ? "selected" : ""; 225 226 <li> 227 <button type="button" class="btn dropdown-item" data-value="@unitOption.Id" onclick="document.querySelector('#UnitSelectorForm_@(product.Id)_@(product.VariantId.Replace(".", "_"))_@Model.ID').querySelector('.js-unit-id').value = this.getAttribute('data-value'); 228 document.querySelector('#Unit_@(product.Id)_@product.VariantId.Replace(".", "_")').value = this.getAttribute('data-value'); 229 swift.PageUpdater.Update(document.querySelector('#UnitSelectorForm_@(product.Id)_@(product.VariantId.Replace(".", "_"))_@Model.ID'))"> 230 <span>@unitOption.Name</span> 231 <span> 232 @if (unitOption.StockLevel > 0 || unitOption.NeverOutOfStock) 233 { 234 if (!Model.Item.GetBoolean("HideInventory") && !unitOption.NeverOutOfStock) 235 { 236 <span class="small text-success">@unitOption.StockLevel @Translate("In stock")</span> 237 } 238 else 239 { 240 <span class="small text-success">@Translate("In stock")</span> 241 } 242 } 243 else 244 { 245 <span class="small text-danger">@Translate("Out of Stock")</span> 246 } 247 </span> 248 </button> 249 </li> 250 } 251 </ul> 252 </div> 253 <button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary @(buttonSize) js-add-to-cart-button" style="white-space: nowrap" @disableAddToCart title="@Translate("Add to cart")" id="AddToCartButton@(product.Id)_@Pageview.CurrentParagraph.ID"> 254 @if (!Model.Item.GetBoolean("HideButtonText")) 255 { 256 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2"> 257 @addToCartLabel 258 </span> 259 } 260 else 261 { 262 @addToCartLabel 263 } 264 </button> 265 </div> 266 } 267 else 268 { 269 if (!anonymousUser && favoritesSelector) 270 { 271 @RenderPartial("Components/ToggleFavorite.cshtml", product) 272 } 273 274 <div class="input-group input-primary-button-group flex-nowrap@(inputSize)"> 275 @if (quantitySelector) 276 { 277 <input id="Quantity_@(product.Id)_@product.VariantId.Replace(".", "_")" name="Quantity" value="@valueQty" step="@stepQty" min="@minQty" max="@maxQty" class="form-control swift_quantity-field" style="min-width: 60px; max-width: 100px; z-index: 1" type="number" @disableAddToCart> 278 } 279 280 <button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary @(buttonSize) @flexFill js-add-to-cart-button" style="white-space: nowrap" @disableAddToCart title="@Translate("Add to cart")" id="AddToCartButton@(product.Id)_@Pageview.CurrentParagraph.ID"> 281 @if (!Model.Item.GetBoolean("HideButtonText")) 282 { 283 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2"> 284 @addToCartLabel 285 </span> 286 } 287 else 288 { 289 @addToCartLabel 290 } 291 </button> 292 </div> 293 } 294 </div> 295 </form> 296 </div> 297 } else if (whenVariantsExist == "modal") { 298 string ButtonShape = Model.Item.GetRawValueString("VariantButtonShape", "square"); 299 string buttonAspectRatio = Model.Item.GetRawValueString("VariantImageAspectRatio", "56%"); 300 301 string buttonText = Translate("Select"); 302 string variantId = !string.IsNullOrWhiteSpace(product.VariantId) ? product.VariantId : product.DefaultVariantId; 303 304 string variantSelectorServicePageId = !string.IsNullOrEmpty(Model.Item.GetString("VariantSelectorServicePageId")) ? Model.Item.GetLink("VariantSelectorServicePageId").PageId.ToString() : ""; 305 variantSelectorServicePageId = variantSelectorServicePageId != "" ? variantSelectorServicePageId : GetPageIdByNavigationTag("VariantSelectorService").ToString(); 306 307 <div class="d-flex @horizontalAlign w-100 item_@Model.Item.SystemName.ToLower()"> 308 @if (!anonymousUser && favoritesSelector) 309 { 310 @RenderPartial("Components/ToggleFavorite.cshtml", product) 311 } 312 <form action="/Default.aspx?ID=@variantSelectorServicePageId" data-response-target-element="DynamicModalContent" data-preloader="inline" style="z-index: 1" class="@fullWidth"> 313 <input type="hidden" name="ProductID" value="@product.Id"> 314 <input type="hidden" name="VariantID" value="@variantId"> 315 <input type="hidden" name="QuantitySelector" value="@quantitySelector.ToString()"> 316 <input type="hidden" name="HideInventory" value="@hideInventory.ToString()"> 317 <input type="hidden" name="HideStockState" value="@hideStockState.ToString()"> 318 <input type="hidden" name="ButtonLayout" value="@ButtonShape"> 319 <input type="hidden" name="ButtonAspectRatio" value="@buttonAspectRatio"> 320 <input type="hidden" name="VariantSelectorServicePage" value="@variantSelectorServicePageId"> 321 <input type="hidden" name="ViewType" value="ModalContent"> 322 @if (isLazyLoadingForProductInfoEnabled) 323 { 324 @* If lazy loading is enabled, bypass it because we're loading a modal window, so render everything as if it was server-side *@ 325 <input type="hidden" name="getproductinfo" value="true"> 326 } 327 <button type="button" onclick="swift.PageUpdater.Update(event)" class="btn btn-primary@(buttonSize) @fullWidth" title="@Translate("Select")" data-bs-toggle="modal" data-bs-target="#DynamicModal" id="OpenVariantSelectorModal@(product.Id)_@Pageview.CurrentParagraph.ID">@buttonText</button> 328 </form> 329 </div> 330 } 331 } 332 } else if (Pageview.IsVisualEditorMode) { 333 <div class="alert alert-dark m-0">@Translate("No products available")</div> 334 } 335