0

我正在开发 nopCommerce v3.90。我需要一个插件,该插件将根据插件设置部分中执行的设置按一定百分比更新产品的原始价格,而不改变现有的 nopCommerce 模型结构。

因此,每次显示产品价格时,应该能够看到新的更新价格(基于插件中执行的操作)而不是数据库中的价格。

谁能帮我这个?

nopCommerce 中的现有模型类

public partial class ProductPriceModel : BaseNopModel
{
    //block of statements

    public string Price { get; set; } //the property whose value need to be changed from plugin

    //block of statements
}
4

1 回答 1

2

3.9中,我知道的选项是

  • 覆盖类PrepareProductPriceModel中的方法IProductModelFactory并使用依赖项覆盖提供您的自定义实现
  • 在视图中使用它之前实现一个ActionFilter自定义。ProductPriceModel

4.0中,这很容易。您只需订阅ModelPreparedEvent然后自定义ProductPriceModel.


覆盖 IProductModelFactory

public class CustomProductModelFactory : ProductModelFactory
{    
    // ctor ....

    protected override ProductDetailsModel.ProductPriceModel PrepareProductPriceModel(Product product)
    {
        // your own logic to prepare price model
    }    
}

在您的插件依赖注册器中

builder.RegisterType<CustomProductModelFactory>()
       .As<IProductModelFactory>()
       .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
       .InstancePerLifetimeScope();

实施 ActionFilter

public class PriceInterceptor : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext == null) throw new ArgumentNullException(nameof(filterContext));    
        if (filterContext.HttpContext?.Request == null) return;

        if (filterContext.Controller is Controller controller)
        {
            if (controller.ViewData.Model is ProductDetailsModel model)
            {
                // your own logic to prepare price model
            }
        }
    }
}

并动态提供您的 ActionFilter

public class PriceInterceptorFilterProvider : IFilterProvider
{
    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        return new[] { new Filter(new PriceInterceptor(), FilterScope.Action, null) };
    }
}

在您的插件依赖注册器中

builder.RegisterType<PriceInterceptorFilterProvider>()
       .As<IFilterProvider>();

订阅 ModelPreparedEvent<ProductDetailsModel>(nopCommerce 4.0)

public class PriceInterceptor : IConsumer<ModelPreparedEvent<ProductDetailsModel>>
{
    public void HandleEvent(ModelPreparedEvent<ProductDetailsModel> eventMessage)
    {
        // your own logic to prepare price model
    }
}
于 2018-03-28T11:33:40.340 回答