protected ProductModel.ProductVariantModel PrepareProductVariantModel(ProductModel.ProductVariantModel model, ProductVariant productVariant) { if (productVariant == null) throw new ArgumentNullException("productVariant"); if (model == null) throw new ArgumentNullException("model"); #region Properties model.Id = productVariant.Id; model.Name = productVariant.GetLocalized(x => x.Name); model.ShowSku = _catalogSettings.ShowProductSku; model.Sku = productVariant.Sku; model.Description = productVariant.GetLocalized(x => x.Description); model.ShowManufacturerPartNumber = _catalogSettings.ShowManufacturerPartNumber; model.ManufacturerPartNumber = productVariant.ManufacturerPartNumber; model.ShowGtin = _catalogSettings.ShowGtin; model.Gtin = productVariant.Gtin; model.StockAvailablity = productVariant.FormatStockMessage(_localizationService); model.PictureModel.FullSizeImageUrl = _pictureService.GetPictureUrl(productVariant.PictureId, 0, false); model.PictureModel.ImageUrl = _pictureService.GetPictureUrl(productVariant.PictureId, _mediaSetting.ProductVariantPictureSize, false); model.PictureModel.Title = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"), model.Name); model.PictureModel.AlternateText = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"), model.Name); if (productVariant.IsDownload && productVariant.HasSampleDownload) { model.DownloadSampleUrl = Url.Action("Sample", "Download", new { productVariantId = productVariant.Id }); } model.IsCurrentCustomerRegistered = _workContext.CurrentCustomer.IsRegistered(); //back in stock subscriptions) if (productVariant.ManageInventoryMethod == ManageInventoryMethod.ManageStock && productVariant.BackorderMode == BackorderMode.NoBackorders && productVariant.AllowBackInStockSubscriptions && productVariant.StockQuantity <= 0) { //out of stock model.DisplayBackInStockSubscription = true; model.BackInStockAlreadySubscribed = _backInStockSubscriptionService.FindSubscription(_workContext.CurrentCustomer.Id, productVariant.Id) != null; } #endregion #region Product variant price model.ProductVariantPrice.ProductVariantId = productVariant.Id; model.ProductVariantPrice.DynamicPriceUpdate = _catalogSettings.EnableDynamicPriceUpdate; if (_permissionService.Authorize(StandardPermissionProvider.DisplayPrices)) { model.ProductVariantPrice.HidePrices = false; if (productVariant.CustomerEntersPrice) { model.ProductVariantPrice.CustomerEntersPrice = true; } else { if (productVariant.CallForPrice) { model.ProductVariantPrice.CallForPrice = true; } else { decimal taxRate = decimal.Zero; decimal oldPriceBase = _taxService.GetProductPrice(productVariant, productVariant.OldPrice, out taxRate); decimal finalPriceWithoutDiscountBase = _taxService.GetProductPrice(productVariant, _priceCalculationService.GetFinalPrice(productVariant, false), out taxRate); decimal finalPriceWithDiscountBase = _taxService.GetProductPrice(productVariant, _priceCalculationService.GetFinalPrice(productVariant, true), out taxRate); decimal oldPrice = _currencyService.ConvertFromPrimaryStoreCurrency(oldPriceBase, _workContext.WorkingCurrency); decimal finalPriceWithoutDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceWithoutDiscountBase, _workContext.WorkingCurrency); decimal finalPriceWithDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceWithDiscountBase, _workContext.WorkingCurrency); if (finalPriceWithoutDiscountBase != oldPriceBase && oldPriceBase > decimal.Zero) model.ProductVariantPrice.OldPrice = _priceFormatter.FormatPrice(oldPrice); model.ProductVariantPrice.Price = _priceFormatter.FormatPrice(finalPriceWithoutDiscount); if (finalPriceWithoutDiscountBase != finalPriceWithDiscountBase) model.ProductVariantPrice.PriceWithDiscount = _priceFormatter.FormatPrice(finalPriceWithDiscount); model.ProductVariantPrice.PriceValue = finalPriceWithoutDiscount; model.ProductVariantPrice.PriceWithDiscountValue = finalPriceWithDiscount; } } } else { model.ProductVariantPrice.HidePrices = true; model.ProductVariantPrice.OldPrice = null; model.ProductVariantPrice.Price = null; } #endregion #region 'Add to cart' model model.AddToCart.ProductVariantId = productVariant.Id; //quantity model.AddToCart.EnteredQuantity = productVariant.OrderMinimumQuantity; //'add to cart', 'add to wishlist' buttons model.AddToCart.DisableBuyButton = productVariant.DisableBuyButton || !_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart); model.AddToCart.DisableWishlistButton = productVariant.DisableWishlistButton || !_permissionService.Authorize(StandardPermissionProvider.EnableWishlist); if (!_permissionService.Authorize(StandardPermissionProvider.DisplayPrices)) { model.AddToCart.DisableBuyButton = true; model.AddToCart.DisableWishlistButton = true; } //pre-order model.AddToCart.AvailableForPreOrder = productVariant.AvailableForPreOrder; //customer entered price model.AddToCart.CustomerEntersPrice = productVariant.CustomerEntersPrice; if (model.AddToCart.CustomerEntersPrice) { decimal minimumCustomerEnteredPrice = _currencyService.ConvertFromPrimaryStoreCurrency(productVariant.MinimumCustomerEnteredPrice, _workContext.WorkingCurrency); decimal maximumCustomerEnteredPrice = _currencyService.ConvertFromPrimaryStoreCurrency(productVariant.MaximumCustomerEnteredPrice, _workContext.WorkingCurrency); model.AddToCart.CustomerEnteredPrice = minimumCustomerEnteredPrice; model.AddToCart.CustomerEnteredPriceRange = string.Format(_localizationService.GetResource("Products.EnterProductPrice.Range"), _priceFormatter.FormatPrice(minimumCustomerEnteredPrice, false, false), _priceFormatter.FormatPrice(maximumCustomerEnteredPrice, false, false)); } #endregion #region Gift card model.GiftCard.IsGiftCard = productVariant.IsGiftCard; if (model.GiftCard.IsGiftCard) { model.GiftCard.GiftCardType = productVariant.GiftCardType; model.GiftCard.SenderName = _workContext.CurrentCustomer.GetFullName(); model.GiftCard.SenderEmail = _workContext.CurrentCustomer.Email; } #endregion #region Product attributes var productVariantAttributes = _productAttributeService.GetProductVariantAttributesByProductVariantId(productVariant.Id); foreach (var attribute in productVariantAttributes) { var pvaModel = new ProductModel.ProductVariantModel.ProductVariantAttributeModel() { Id = attribute.Id, ProductVariantId = productVariant.Id, ProductAttributeId = attribute.ProductAttributeId, Name = attribute.ProductAttribute.GetLocalized(x => x.Name), Description = attribute.ProductAttribute.GetLocalized(x => x.Description), TextPrompt = attribute.TextPrompt, IsRequired = attribute.IsRequired, AttributeControlType = attribute.AttributeControlType, }; if (attribute.ShouldHaveValues()) { //values var pvaValues = _productAttributeService.GetProductVariantAttributeValues(attribute.Id); foreach (var pvaValue in pvaValues) { var pvaValueModel = new ProductModel.ProductVariantModel.ProductVariantAttributeValueModel() { Id = pvaValue.Id, Name = pvaValue.GetLocalized(x=>x.Name), IsPreSelected = pvaValue.IsPreSelected, }; pvaModel.Values.Add(pvaValueModel); //display price if allowed if (_permissionService.Authorize(StandardPermissionProvider.DisplayPrices)) { decimal taxRate = decimal.Zero; decimal priceAdjustmentBase = _taxService.GetProductPrice(productVariant, pvaValue.PriceAdjustment, out taxRate); decimal priceAdjustment = _currencyService.ConvertFromPrimaryStoreCurrency(priceAdjustmentBase, _workContext.WorkingCurrency); if (priceAdjustmentBase > decimal.Zero) pvaValueModel.PriceAdjustment = "+" + _priceFormatter.FormatPrice(priceAdjustment, false, false); else if (priceAdjustmentBase < decimal.Zero) pvaValueModel.PriceAdjustment = "-" + _priceFormatter.FormatPrice(-priceAdjustment, false, false); pvaValueModel.PriceAdjustmentValue = priceAdjustment; } } } model.ProductVariantAttributes.Add(pvaModel); } #endregion return model; }
//product public static ProductModel ToModel(this Product entity) { if (entity == null) return null; var model = new ProductModel() { Id = entity.Id, Name = entity.GetLocalized(x => x.Name), ShortDescription = entity.GetLocalized(x => x.ShortDescription), FullDescription = entity.GetLocalized(x => x.FullDescription), MetaKeywords = entity.GetLocalized(x => x.MetaKeywords), MetaDescription = entity.GetLocalized(x => x.MetaDescription), MetaTitle = entity.GetLocalized(x => x.MetaTitle), SeName = entity.GetSeName(), }; return model; }