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