Overriding ProductController and ProductDetailsModel

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 年 前
I want to override ProductController, ProductDetailsModel and ProductTemplate.Simple.cshtml in a plugin.

The new model in the plugin is inherited from ProductDetailsModel and this is how it looks:

namespace Nop.Plugin.Misc.WarehouseStockAvailability.Models
{
    public partial class ProductNewModel : ProductDetailsModel
    {
        public ProductNewModel()
        {
            ProductWarehouseInventoryModels = new List<ProductWarehouseInventoryModel>();
            AvailableWarehouses = new List<SelectListItem>();
        }

        [NopResourceDisplayName("Admin.Catalog.Products.Fields.Warehouse")]
        public int WarehouseId { get; set; }
        public IList<SelectListItem> AvailableWarehouses { get; set; }

        //multiple warehouses
        [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory")]
        public IList<ProductWarehouseInventoryModel> ProductWarehouseInventoryModels { get; set; }


        #region Nested Classes

        public partial class ProductWarehouseInventoryModel : BaseNopModel
        {
            [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory.Fields.Warehouse")]
            public int WarehouseId { get; set; }
            [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory.Fields.Warehouse")]
            public string WarehouseName { get; set; }

            [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory.Fields.WarehouseUsed")]
            public bool WarehouseUsed { get; set; }

            [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory.Fields.StockQuantity")]
            public int StockQuantity { get; set; }

            [NopResourceDisplayName("Admin.Catalog.Products.ProductWarehouseInventory.Fields.ReservedQuantity")]
            public int ReservedQuantity { get; set; }


            public int WarehouseStockQty;

        }

        #endregion
    }
}



The controller in the plugin is inherited from ProductController and here I'm overriding PrepareProductDetailsPageModel.

protected override ProductDetailsModel PrepareProductDetailsPageModel(Product product,
            ShoppingCartItem updatecartitem = null, bool isAssociatedProduct = false)
        {
      var model = new ProductNewModel
            {
                Id = product.Id,
                Name = product.GetLocalized(x => x.Name),
                ShortDescription = product.GetLocalized(x => x.ShortDescription),
                FullDescription = product.GetLocalized(x => x.FullDescription),
                MetaKeywords = product.GetLocalized(x => x.MetaKeywords),
                MetaDescription = product.GetLocalized(x => x.MetaDescription),
                MetaTitle = product.GetLocalized(x => x.MetaTitle),
                SeName = product.GetSeName(),
                ProductType = product.ProductType,
                ShowSku = _catalogSettings.ShowProductSku,
                Sku = product.Sku,
                ShowManufacturerPartNumber = _catalogSettings.ShowManufacturerPartNumber,
                FreeShippingNotificationEnabled = _catalogSettings.ShowFreeShippingNotification,
                ManufacturerPartNumber = product.ManufacturerPartNumber,
                ShowGtin = _catalogSettings.ShowGtin,
                Gtin = product.Gtin,
                StockAvailability = product.FormatStockMessage("", _localizationService, _productAttributeParser),
                HasSampleDownload = product.IsDownload && product.HasSampleDownload,
                DisplayDiscontinuedMessage = !product.Published && _catalogSettings.DisplayDiscontinuedMessageForUnpublishedProducts
            };
      ........
            ........
            return model;
  }


In the ProductTemplate.Simple.cshtml view in the plugin I'm adding a new partial view with the new model.
@{
    var newModel = new ProductNewModel()
}
@Html.Partial("_NewPartialView", newModel)

The problem is that I'm always geting 0 for "newModel.ProductWarehouseInventoryModels.Count"

If someone can help me to figure ou what I'm doing wrong would be great?
7 年 前
marijag wrote:
The controller in the plugin is inherited from ProductController and here I'm overriding PrepareProductDetailsPageModel.

protected override ProductDetailsModel PrepareProductDetailsPageModel(Product product,
            ShoppingCartItem updatecartitem = null, bool isAssociatedProduct = false)
        {
      var model = new ProductNewModel
            {
                Id = product.Id,
                Name = product.GetLocalized(x => x.Name),
                ShortDescription = product.GetLocalized(x => x.ShortDescription),
                FullDescription = product.GetLocalized(x => x.FullDescription),
                MetaKeywords = product.GetLocalized(x => x.MetaKeywords),
                MetaDescription = product.GetLocalized(x => x.MetaDescription),
                MetaTitle = product.GetLocalized(x => x.MetaTitle),
                SeName = product.GetSeName(),
                ProductType = product.ProductType,
                ShowSku = _catalogSettings.ShowProductSku,
                Sku = product.Sku,
                ShowManufacturerPartNumber = _catalogSettings.ShowManufacturerPartNumber,
                FreeShippingNotificationEnabled = _catalogSettings.ShowFreeShippingNotification,
                ManufacturerPartNumber = product.ManufacturerPartNumber,
                ShowGtin = _catalogSettings.ShowGtin,
                Gtin = product.Gtin,
                StockAvailability = product.FormatStockMessage("", _localizationService, _productAttributeParser),
                HasSampleDownload = product.IsDownload && product.HasSampleDownload,
                DisplayDiscontinuedMessage = !product.Published && _catalogSettings.DisplayDiscontinuedMessageForUnpublishedProducts
            };
      ........
            ........
            return model;
  }

The problem is that I'm always geting 0 for "newModel.ProductWarehouseInventoryModels.Count"

If someone can help me to figure out what I'm doing wrong would be great?

Your overridden PrepareProductDetailsPageModel function is still returning a model of type ProductDetailsModel, I think it should be returning a model of your new ProductNewModel type. You also don't have any code in the function to actually populate the AvailableWarehouses and ProductWarehouseInventoryModel properties that you added.

marijag wrote:
In the ProductTemplate.Simple.cshtml view in the plugin I'm adding a new partial view with the new model.
@{
    var newModel = new ProductNewModel()
}
@Html.Partial("_NewPartialView", newModel)

The ProductNewModel instance that you pass to your partial view needs to be the one that the controller returned to the parent view. You can't just create a new one and expect it to be populated.
7 年 前
Thank for answer me. This is the code that I have in the function and for that I have added the AvailableWarehouses and ProductWarehouseInventoryModel properties.

// **************** Added in the new controller in function PrepareProductDetailsPageModel ********************

            //warehouses
            var warehouses = _shippingService.GetAllWarehouses();
            model.AvailableWarehouses.Add(new SelectListItem
            {
                Text = _localizationService.GetResource("Admin.Catalog.Products.Fields.Warehouse.None"),
                Value = "0"
            });
            foreach (var warehouse in warehouses)
            {
                model.AvailableWarehouses.Add(new SelectListItem
                {
                    Text = warehouse.Name,
                    Value = warehouse.Id.ToString()
                });
            }

            //multiple warehouses
            foreach (var warehouse in warehouses)
            {
                var pwiModel = new ProductNewModel.ProductWarehouseInventoryModel
                {
                    WarehouseId = warehouse.Id,
                    WarehouseName = warehouse.Name
                };
                if (product != null)
                {
                    var pwi = product.ProductWarehouseInventory.FirstOrDefault(x => x.WarehouseId == warehouse.Id);
                    if (pwi != null)
                    {
                        pwiModel.WarehouseUsed = true;
                        pwiModel.StockQuantity = pwi.StockQuantity;
                        pwiModel.ReservedQuantity = pwi.ReservedQuantity;
                        pwiModel.WarehouseStockQty = pwi.StockQuantity - pwi.ReservedQuantity;
                    }
                }
                model.ProductWarehouseInventoryModels.Add(pwiModel);
            }

            // **************** Added in the new controller in function PrepareProductDetailsPageModel ********************


I'm very new in this so if you could tell me how can I use the new model only for the new partial view?

Thanks, Marija.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.