Komponent editor til: Product grid card - Mobile - Compressed

Error executing template "/Designs/Swift/Paragraph/Swift_ProductAddToCart_Custom.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at Smartpage.PhilipsonWine.Ecommerce.ProductInformation.ProductInformation..ctor(ProductViewModel product) in D:\a\1\s\Smartpage.PhilipsonWine.Ecommerce\ProductInformation\ProductInformation.cs:line 85
   at CompiledRazorTemplates.Dynamic.RazorEngine_c83c17681e924683a51980986d989c70.Execute() in D:\dynamicweb.net\Solutions\Wineshop\Files\Templates\Designs\Swift\Paragraph\Swift_ProductAddToCart_Custom.cshtml:line 37
   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 @*Custom code*@ 5 @using Smartpage.PhilipsonWine.Ecommerce.ProductInformation 6 @using Dynamicweb.Core 7 @using Smartpage.PhilipsonWine.EcomPrices 8 @using Smartpage.PhilipsonWine.EcomPrices.Models 9 @using Smartpage.Tracking.Helper 10 @using System 11 @using System.Web 12 13 14 @{ 15 ProductViewModel product = null; 16 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 17 { 18 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 19 } 20 else if (Pageview.Item["DummyProduct"] != null) 21 { 22 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page); 23 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel(); 24 25 if (productList?.Products is object) 26 { 27 product = productList.Products[0]; 28 } 29 } 30 31 string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", ""); 32 bool anonymousUser = Pageview.User == null; 33 bool isErpConnectionDown = !Dynamicweb.Core.Converter.ToBoolean(Dynamicweb.Context.Current.Items["IsWebServiceConnectionAvailable"]); 34 bool hideAddToCart = anonymousUsersLimitations.Contains("cart") && anonymousUser || Pageview.AreaSettings.GetBoolean("ErpDownHideAddToCart") && isErpConnectionDown; 35 hideAddToCart = Pageview.IsVisualEditorMode ? false : hideAddToCart; 36 //Custom code 37 var productField = new ProductInformation(product); 38 var kolli = productField.CurrentPriceQuantity; 39 string productLink = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl("Default.aspx?Id=" + GetPageIdByNavigationTag("Shop") + "&ProductId=" + product.Id); 40 string productImage = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + product.DefaultImage.Value; 41 var currentUser = Dynamicweb.Security.UserManagement.User.GetCurrentExtranetUser(); 42 CustomPrice customPrice = null; 43 CustomPriceService.GetCustomPriceProducts(currentUser)?.TryGetValue(product.Id, out customPrice); 44 int priceQuantity = customPrice?.PriceQuantity != null && customPrice?.PriceQuantity > 0 ? Converter.ToInt32(customPrice?.PriceQuantity) : productField.MaxPriceBasedOn; 45 string shopId = Pageview.Area.EcomShopId; 46 var priceObject = new Smartpage.PhilipsonWine.Ecommerce.Prices.PriceObject(product.Id, priceQuantity, product.Discount.Price, productField.OnSale, Dynamicweb.Ecommerce.Common.Context.Currency, Dynamicweb.Ecommerce.Common.Context.Country, product.LanguageId, shopId); 47 var productDiscount = PriceHelper.FormatPrice(priceObject.NormalPrice - priceObject.DiscountPrice); 48 string areaId = Converter.ToString(Pageview.AreaID); 49 string time = DateTime.Now.ToString("G", System.Globalization.CultureInfo.InvariantCulture); 50 string uniqueId = Guid.NewGuid().ToString("N"); 51 } 52 53 @if (product is object && !hideAddToCart) 54 { 55 string horizontalAlign = Model.Item.GetRawValueString("HorizontalAlignment", ""); 56 horizontalAlign = horizontalAlign == "center" ? "justify-content-center" : horizontalAlign; 57 horizontalAlign = horizontalAlign == "end" ? "justify-content-end" : horizontalAlign; 58 horizontalAlign = horizontalAlign == "full" ? "" : horizontalAlign; 59 60 bool favoritesSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowAddToFavorites")) ? Model.Item.GetBoolean("ShowAddToFavorites") : false; 61 bool quantitySelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowQuantitySelector")) ? Model.Item.GetBoolean("ShowQuantitySelector") : false; 62 bool unitsSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowUnitsSelector")) ? Model.Item.GetBoolean("ShowUnitsSelector") : false; 63 bool hideInventory = !string.IsNullOrEmpty(Model.Item.GetString("HideInventory")) ? Model.Item.GetBoolean("HideInventory") : false; 64 bool hideStockState = !string.IsNullOrEmpty(Model.Item.GetString("HideStockState")) ? Model.Item.GetBoolean("HideStockState") : false; 65 66 string buttonSize = Model.Item.GetRawValueString("ButtonSize", "regular"); 67 string inputSize = string.Empty; 68 69 switch (buttonSize) 70 { 71 case "small": 72 inputSize = " input-group-sm"; 73 buttonSize = " btn-sm"; 74 break; 75 case "regular": 76 buttonSize = string.Empty; 77 break; 78 case "large": 79 inputSize = " input-group-lg"; 80 buttonSize = " btn-lg"; 81 break; 82 } 83 84 string iconPath = "/Files/icons/"; 85 string url = "/Default.aspx?ID=" + (GetPageIdByNavigationTag("CartService")); 86 if (!url.Contains("LayoutTemplate")) 87 { 88 url += url.Contains("?") ? "&LayoutTemplate=Swift_MiniCart.cshtml" : "?LayoutTemplate=Swift_MiniCart.cshtml"; 89 } 90 91 bool outOfStock = (product.StockLevel <= 0); 92 string hideColliClass = outOfStock ? "d-none" : ""; 93 string disableAddToCart = outOfStock ? "disabled" : ""; 94 bool isNeverOutOfStock = product.NeverOutOfstock; 95 disableAddToCart = isNeverOutOfStock ? "" : disableAddToCart; 96 97 string soldOutBorderRadiusClass = outOfStock ? "sold-out-button" : ""; 98 99 string whenVariantsExist = Model.Item.GetRawValueString("WhenVariantsExist", "hide"); 100 101 string flexFill = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "flex-fill" : ""; 102 string fullWidth = Model.Item.GetRawValueString("HorizontalAlignment", "") == "full" ? "w-100" : ""; 103 string addToCartIcon = Model.Item.GetRawValueString("Icon", iconPath + "shopping-cart.svg"); 104 string addToCartLabel = !addToCartIcon.Contains("_none") ? "<span class=\"icon-2\">" + ReadFile(addToCartIcon) + "</span>" : ""; 105 addToCartLabel += !addToCartIcon.Contains("_none") && !Model.Item.GetBoolean("HideButtonText") ? " " : ""; 106 addToCartLabel += !Model.Item.GetBoolean("HideButtonText") && !outOfStock ? Translate("Add to cart") : Translate("Sold out"); 107 108 if (product.VariantInfo.VariantInfo == null || whenVariantsExist == "disable") 109 { 110 string unitId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("UnitId")) ? Dynamicweb.Context.Current.Request.Form.Get("UnitId") : product.DefaultUnitId; 111 if (string.IsNullOrEmpty(unitId) && product?.UnitOptions != null) 112 { 113 if (product.UnitOptions.FirstOrDefault<UnitOptionViewModel>() != null) 114 { 115 unitId = product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Id; 116 } 117 } 118 119 string minQty = product.PurchaseMinimumQuantity != 1 ? "min=\"" + product.PurchaseMinimumQuantity.ToString() + "\"" : "min=\"1\""; 120 string stepQty = product.PurchaseQuantityStep > 1 ? product.PurchaseQuantityStep.ToString() : "1"; 121 string valueQty = product.PurchaseMinimumQuantity > product.PurchaseQuantityStep ? product.PurchaseMinimumQuantity.ToString() : stepQty; 122 disableAddToCart = product.VariantInfo.VariantInfo != null && string.IsNullOrEmpty(product.VariantId) ? "disabled" : disableAddToCart; 123 124 var reserveMode = Dynamicweb.Ecommerce.Frontend.Cart.ProductReserve.Mode; 125 126 if (unitsSelector && product.UnitOptions.Count > 0) 127 { 128 <form method="post" action="/Default.aspx?ID=@(Pageview.Page.ID)&ProductId=@product.Id" id="UnitSelectorForm_@(product.Id)_@(product.VariantId)_@Model.ID"> 129 <input type="hidden" name="redirect" value="false"> 130 <input type="hidden" name="VariantID" value="@product.VariantId"> 131 <input type="hidden" name="UnitID" class="js-unit-id" value="@unitId"> 132 </form> 133 } 134 135 <div class="d-flex @horizontalAlign @fullWidth js-input-group item_@Model.Item.SystemName.ToLower()"> 136 <form method="post" action="@url" class="@fullWidth" style="z-index: 1"> 137 <input type="hidden" name="redirect" value="false"> 138 <input type="hidden" name="ProductId" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(product.Id)"> 139 <input type="hidden" name="ProductName" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(product.Name)"> 140 <input type="hidden" name="ProductVariantName" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(product.VariantName)"> 141 <input type="hidden" name="ProductCurrency" value="@Dynamicweb.Ecommerce.Common.Context.Currency.Code"> 142 <input type="hidden" name="ProductPrice" value="@Converter.ToInt32(PriceViewModelExtensions.ToStringInvariant(product.Price))"> 143 <input type="hidden" name="ProductReferer" value="component_ProductAddToCart"> 144 <input type="hidden" name="cartcmd" value="add"> 145 @*Custom code*@ 146 <input type="hidden" name="ProductBrand" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.Brand)"> 147 <input type="hidden" name="ProductType" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.Type)"> 148 <input type="hidden" name="ProductPrimaryGrape" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.PrimaryGrape)"> 149 <input type="hidden" name="ProductCountry" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.Country)"> 150 <input type="hidden" name="ProductRegion" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.Area)"> 151 <input type="hidden" name="ProductCounty" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.County)"> 152 <input type="hidden" name="ProductVintage" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productField.Year)"> 153 <input type="hidden" name="ProductOnSale" value="@productField.OnSale.ToString()"> 154 <input type="hidden" name="ProductBottleSize" value="@productField.BottleSizeCl"> 155 <input type="hidden" name="ProductLink" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productLink)"> 156 <input type="hidden" name="ProductImage" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productImage)"> 157 <input type="hidden" name="ProductDiscount" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(productDiscount)"> 158 <input type="hidden" name="ShopId" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(shopId)"> 159 <input type="hidden" name="AreaId" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(areaId)"> 160 <input type="hidden" name="Time" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(time)"> 161 <input type="hidden" name="UniqueId" value="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(uniqueId)"> 162 163 @if (reserveMode == Dynamicweb.Ecommerce.Frontend.Cart.ProductReserveMode.AddToCart) 164 { 165 <input type="hidden" name="GetReservedAmount" value="true"> 166 } 167 168 @if (!string.IsNullOrEmpty(product.VariantId)) 169 { 170 <input type="hidden" name="VariantId" value="@product.VariantId"> 171 } 172 173 @if (!product.NeverOutOfstock) 174 { 175 <input type="hidden" name="Stock" value="@product.StockLevel"> 176 177 <template class="js-out-of-stock-notice"> 178 <div class="modal-header"> 179 <h1 class="modal-title fs-5">@Translate("Stock limit")</h1> 180 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> 181 </div> 182 <div class="modal-body"> 183 @Translate("There are not enough products in stock. The product might be sold out or discontinued. Please adjust the quantity.") 184 </div> 185 </template> 186 } 187 188 @if (stepQty != "1") 189 { 190 <template class="js-step-quantity-warning"> 191 <div class="modal-header"> 192 <h1 class="modal-title fs-5">@Translate("The quantity is not valid")</h1> 193 </div> 194 <div class="modal-body"> 195 @Translate("Please select a quantity that is dividable by") @stepQty 196 </div> 197 </template> 198 } 199 @if (product.PurchaseMinimumQuantity != 1) 200 { 201 <template class="js-min-quantity-warning"> 202 <div class="modal-header"> 203 <h1 class="modal-title fs-5">@Translate("The product could not be added to the cart")</h1> 204 </div> 205 <div class="modal-body"> 206 @Translate("The quantity is not valid. You must buy at least") @product.PurchaseMinimumQuantity 207 </div> 208 </template> 209 } 210 211 @if (quantitySelector || (!anonymousUser && product.VariantInfo.VariantInfo != null) || (!anonymousUser && favoritesSelector)) 212 { 213 <input type="hidden" id="Unit_@(product.Id)_@product.VariantId" name="UnitID" value="@unitId" /> 214 } 215 216 <div class="d-flex flex-row w-100"> 217 @if (!anonymousUser && favoritesSelector) 218 { 219 @RenderPartial("Components/ToggleFavorite.cshtml", product) 220 } 221 222 @if (!quantitySelector) 223 { 224 <input id="Quantity_@(product.Id)_@product.VariantId" name="Quantity" value="@kolli" type="hidden" class="@hideColliClass">//Custom code 225 } 226 227 <div class="input-group input-primary-button-group flex-nowrap@(inputSize)"> 228 @if (quantitySelector) 229 { 230 <input id="Quantity_@(product.Id)_@product.VariantId" name="Quantity" value="@kolli" step="@stepQty" @minQty class="form-control swift_quantity-field @hideColliClass" style="min-width: 60px; max-width: 100px; z-index: 1" type="number" onchange="swift.Cart.UpdateOnEnterKey(event)" onkeyup="swift.Cart.UpdateOnEnterKey(event)" @disableAddToCart>//Custom code 231 } 232 233 @if (unitsSelector && product.UnitOptions.Count > 0) 234 { 235 string selectedUnitName = !string.IsNullOrEmpty(unitId) && product?.UnitOptions != null ? unitId : product.UnitOptions.FirstOrDefault<UnitOptionViewModel>().Name; 236 237 foreach (var unitOption in product.UnitOptions) 238 { 239 if (unitOption.Id == unitId) 240 { 241 selectedUnitName = unitOption.Name; 242 } 243 } 244 245 <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> 246 @selectedUnitName 247 </button> 248 <ul class="dropdown-menu swift_unit-field"> 249 @foreach (var unitOption in product.UnitOptions) 250 { 251 var selectedUnit = unitOption.Id == unitId ? "selected" : ""; 252 253 <li> 254 <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'); 255 document.querySelector('#Unit_@(product.Id)_@product.VariantId').value = this.getAttribute('data-value'); 256 swift.PageUpdater.Update(document.querySelector('#UnitSelectorForm_@(product.Id)_@(product.VariantId)_@Model.ID'))"> 257 <span>@unitOption.Name</span> 258 <span> 259 @if (unitOption.StockLevel > 0) 260 { 261 if (!Model.Item.GetBoolean("HideInventory")) 262 { 263 <span class="small text-success">@unitOption.StockLevel @Translate("In stock")</span> 264 } 265 else 266 { 267 <span class="small text-success">@Translate("In stock")</span> 268 } 269 } 270 else 271 { 272 <span class="small text-danger">@Translate("Out of Stock")</span> 273 } 274 </span> 275 </button> 276 </li> 277 } 278 </ul> 279 } 280 281 <button type="button" onclick="swift.Cart.Update(event)" class="btn btn-primary @(buttonSize) @flexFill js-add-to-cart-button @soldOutBorderRadiusClass" style="white-space: nowrap;" @disableAddToCart title="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(Translate("Add to cart"))" id="AddToCartButton@(product.Id)_@Pageview.CurrentParagraph.ID"> 282 @if (!Model.Item.GetBoolean("HideButtonText")) 283 { 284 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2"> 285 @addToCartLabel 286 </span> 287 } 288 else 289 { 290 @addToCartLabel 291 } 292 </button> 293 294 </div> 295 </div> 296 </form> 297 </div> 298 } 299 else if (whenVariantsExist == "modal") 300 { 301 string buttonText = Translate("Select"); 302 303 string variantSelectorServicePageId = !string.IsNullOrEmpty(Model.Item.GetString("VariantSelectorServicePageId")) ? Model.Item.GetLink("VariantSelectorServicePageId").PageId.ToString() : ""; 304 variantSelectorServicePageId = variantSelectorServicePageId != "" ? variantSelectorServicePageId : GetPageIdByNavigationTag("VariantSelectorService").ToString(); 305 306 <div class="d-flex @horizontalAlign w-100 item_@Model.Item.SystemName.ToLower()"> 307 @if (!anonymousUser && favoritesSelector) 308 { 309 @RenderPartial("Components/ToggleFavorite.cshtml", product) 310 } 311 <form action="/Default.aspx?ID=@variantSelectorServicePageId" data-response-target-element="DynamicModalContent" data-preloader="inline" style="z-index: 1" class="@fullWidth"> 312 <input type="hidden" name="ProductID" value="@product.Id"> 313 <input type="hidden" name="QuantitySelector" value="@quantitySelector.ToString()"> 314 <input type="hidden" name="HideInventory" value="@hideInventory.ToString()"> 315 <input type="hidden" name="HideStockState" value="@hideStockState.ToString()"> 316 <input type="hidden" name="VariantSelectorServicePage" value="@variantSelectorServicePageId"> 317 <input type="hidden" name="ViewType" value="ModalContent"> 318 <button type="button" onclick="swift.PageUpdater.Update(event)" class="btn btn-primary@(buttonSize) @fullWidth" title="@Dynamicweb.Core.Encoders.HtmlEncoder.HtmlAttributeEncode(Translate("Select"))" data-bs-toggle="modal" data-bs-target="#DynamicModal" id="OpenVariantSelectorModal@(product.Id)_@Pageview.CurrentParagraph.ID">@buttonText</button> 319 </form> 320 </div> 321 } 322 } 323 else if (Pageview.IsVisualEditorMode) 324 { 325 <div class="alert alert-dark m-0">@Translate("No products available")</div> 326 } 327