Override Nop.Service

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 anni tempo fa
Hi,

Good day!

I am currently creating new plugins, and 1 of the requirement is to override the "GetSubTotal" from Nop.Service PriceCalculationService.cs
as if now i created new custom file in my plugins folder "Service/LensPriceCalculationService.cs" and add a dependency registrar

builder.RegisterType<LensPriceCalculationService>()
.As<IPriceCalculationService>().SingleInstance()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

but upon playing the debug didnt trigger in the custom file instead it still trigerring in the Nop.Service.
can you help me to override the "PriceCalculationService"




my Dependency registrar

using Autofac;
using Autofac.Core;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Plugin.Widgets.LensPrescription.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Web.Framework.Mvc;
using Nop.Plugin.Widgets.LensPrescription.Domain;
using Nop.Core.Configuration;
using Nop.Services.Catalog;
using Nop.Plugin.Widgets.LensPrescription.Service;

namespace Nop.Plugin.Widgets.LensPrescription.Infrastructure
{
public class DependencyRegistrar : IDependencyRegistrar
{
private const string CONTEXT_NAME = "nop_object_context_plugin_widget_lensprescription";

public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{

//data context
this.RegisterPluginDataContext<LensPrescriptionObjectContext>(builder, CONTEXT_NAME);

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionVisionRecord>>()
.As<IRepository<LensPrescriptionVisionRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionLensRecord>>()
.As<IRepository<LensPrescriptionLensRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionPresRecord>>()
.As<IRepository<LensPrescriptionPresRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionPackageRecord>>()
.As<IRepository<LensPrescriptionPackageRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionProductPackageRecord>>()
.As<IRepository<LensPrescriptionProductPackageRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

//override required repository with our custom context
builder.RegisterType<EfRepository<LensPrescriptionCartDetailsRecord>>()
.As<IRepository<LensPrescriptionCartDetailsRecord>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();

builder.RegisterType<LensPriceCalculationService>()
.As<IPriceCalculationService>().SingleInstance()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
.InstancePerLifetimeScope();
}

public int Order
{
get { return 100; }
}
}
}





I have change it to partial class and public override but still not triggering.

using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Discounts;
using Nop.Core.Domain.Orders;
using Nop.Services.Catalog;
using Nop.Services.Discounts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Plugin.Widgets.LensPrescription.Service
{
public partial class LensPriceCalculationService : PriceCalculationService, IPriceCalculationService
{
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly IDiscountService _discountService;
private readonly ICategoryService _categoryService;
private readonly IManufacturerService _manufacturerService;
private readonly IProductAttributeParser _productAttributeParser;
private readonly IProductService _productService;
private readonly ICacheManager _cacheManager;
private readonly ShoppingCartSettings _shoppingCartSettings;
private readonly CatalogSettings _catalogSettings;

public LensPriceCalculationService(
IWorkContext workContext,
IStoreContext storeContext,
IDiscountService discountService,
ICategoryService categoryService,
IManufacturerService manufacturerService,
IProductAttributeParser productAttributeParser,
IProductService productService,
ICacheManager cacheManager,
ShoppingCartSettings shoppingCartSettings,
CatalogSettings catalogSettings)
: base(workContext, storeContext, discountService, categoryService,manufacturerService, productAttributeParser, productService, cacheManager, shoppingCartSettings, catalogSettings)
{
_workContext = workContext;
_storeContext=storeContext;
_discountService=discountService;
_categoryService=categoryService;
_manufacturerService = manufacturerService;
_productAttributeParser=productAttributeParser;
_productService=productService;
_cacheManager=cacheManager;
_shoppingCartSettings=shoppingCartSettings;
_catalogSettings = catalogSettings;
}

public override decimal GetSubTotal(ShoppingCartItem shoppingCartItem,
bool includeDiscounts = true)
{
decimal discountAmount;
Discount appliedDiscount;
return GetSubTotal(shoppingCartItem, includeDiscounts, out discountAmount, out appliedDiscount);
}

public override decimal GetSubTotal(ShoppingCartItem shoppingCartItem,
bool includeDiscounts,
out decimal discountAmount,
out Discount appliedDiscount)
{
if (shoppingCartItem == null)
throw new ArgumentNullException("shoppingCartItem");

decimal subTotal;

//unit price
var unitPrice = GetUnitPrice(shoppingCartItem, includeDiscounts,
out discountAmount, out appliedDiscount);

//discount
if (appliedDiscount != null)
{
if (appliedDiscount.MaximumDiscountedQuantity.HasValue &&
shoppingCartItem.Quantity > appliedDiscount.MaximumDiscountedQuantity.Value)
{
//we cannot apply discount for all shopping cart items
var discountedQuantity = appliedDiscount.MaximumDiscountedQuantity.Value;
var discountedSubTotal = unitPrice * discountedQuantity;
discountAmount = discountAmount * discountedQuantity;

var notDiscountedQuantity = shoppingCartItem.Quantity - discountedQuantity;
var notDiscountedUnitPrice = GetUnitPrice(shoppingCartItem, false);
var notDiscountedSubTotal = notDiscountedUnitPrice * notDiscountedQuantity;

subTotal = discountedSubTotal + notDiscountedSubTotal;
}
else
{
//discount is applied to all items (quantity)
//calculate discount amount for all items
discountAmount = discountAmount * shoppingCartItem.Quantity;

subTotal = unitPrice * shoppingCartItem.Quantity;
}
}
else
{
subTotal = unitPrice * shoppingCartItem.Quantity;
}
return subTotal;
}
}
}

April 8, 2016, 9:44 AM
8 anni tempo fa
Please make sure that plugins DependencyRegister.cs has

Public int Order
{
return 100;
}

return order more than DependencyRegister.cs of Nop.Web.Frameworks.

i think other things is good.
8 anni tempo fa
Hi,

I already change the dependency registrar to 100 but still doens't invoked.
what else can i do?

Regards,
Bryan
8 anni tempo fa
Hi,

The above code works, we just need to manually delete the compiled plugins for there are duplicate namespace on the dependency registrar, this cannot be detected by Visual Studio upon clean and rebuilding for it was renamed.

Manually deleting the compiled dll's of plugin in the Nop.Web/Plugins solves the problem.

Thanks,
Bryan
7 anni tempo fa
Bryan,

Your post was helpful in understanding how to override a service call. I'm trying to override GetFinalPrice, but I'm having trouble understanding all the components and files that need to be included in the plugin.  For instance, I've created a Infrastructure/DependencyRegistrar.cs:
namespace Nop.Plugin.Widgets.DavesOverrides.Infrastructure
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        private const string CONTEXT_NAME = "nop_object_context_plugin_widget_davesoverrides";

        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<DavesPriceCalculationService>()
            .As<IPriceCalculationService>().SingleInstance()
            .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
            .InstancePerLifetimeScope();
        }
        public int Order
        {
            get { return 1; }
        }
    }
}


I'm not sure if it is correct.  I've also created a Service/DavesPriceCalculationService.cs in which I override all of the "GetFinalPrice" calls.  I'm not sure if I need to create a RouteProvider.cs file so that Nopcommerce can locate the overrides?

Would you mind if I looked at the plugin that you created?  Possibly zip it up so I could use it for an example.  I hope you wouldn't mind.  I'm just trying to figure out how everything fits together.  Thanks!!

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