Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Components/ProductAttributesViewCompon...

74 lines
2.9 KiB
C#

// File: Plugins/YourCompany.ProductAttributes/Components/ProductAttributesViewComponent.cs
using FruitBank.Common.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Plugin.Misc.FruitBankPlugin.Models;
using Nop.Services.Common;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Components;
namespace Nop.Plugin.Misc.FruitBankPlugin.Components
{
[ViewComponent(Name = "ProductAttributes")]
public class ProductAttributesViewComponent : NopViewComponent
{
private const string NET_WEIGHT_KEY = nameof(IMeasuringAttributeValues.NetWeight);
private const string GROSS_WEIGHT_KEY = nameof(IMeasuringAttributeValues.GrossWeight);
private const string IS_MEASURABLE_KEY = nameof(IMeasuringAttributeValues.IsMeasurable);
private readonly IGenericAttributeService _genericAttributeService;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
public ProductAttributesViewComponent(
IGenericAttributeService genericAttributeService,
IWorkContext workContext,
IStoreContext storeContext)
{
_genericAttributeService = genericAttributeService;
_workContext = workContext;
_storeContext = storeContext;
}
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{
if (additionalData is not ProductModel product)
return Content("");
var model = new ProductAttributesModel
{
ProductId = product.Id
};
//get store scope
var storeScope = await _storeContext.GetCurrentStoreAsync();
if (product.Id > 0)
{
// Load existing values
var dbProduct = new Core.Domain.Catalog.Product { Id = product.Id };
//var dbMesaurable = await _genericAttributeService.GetAttributeAsync<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
//var dbNetWeight = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
//var dbIncomingQuantity = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, "IncomingQuantity", storeScope.Id);
model.IsMeasurable = await _genericAttributeService
.GetAttributeAsync<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
model.NetWeight = await _genericAttributeService
.GetAttributeAsync<decimal?>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
model.IncomingQuantity = await _genericAttributeService
.GetAttributeAsync<int?>(dbProduct, "IncomingQuantity", storeScope.Id);
}
return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model);
}
}
}