Hello All,
I am stuck on one of the issue. Can you please help me on this?
I have two plugins and in both plugins same service override but not override it's method.
My code is.
Plugin1
public class Plugin1PriceCalculationService : PriceCalculationService
{
public Plugin1PriceCalculationService():base()
{
}
public override decimal GetUnitPrice(ShoppingCartItem shoppingCartItem,
bool includeDiscounts,
out decimal discountAmount,
out List<DiscountForCaching> appliedDiscounts)
{ }
}
Plugin2
public class Plugin2PriceCalculationService : PriceCalculationService
{
public Plugin2PriceCalculationService():base()
{
}
public override decimal GetSubTotal(ShoppingCartItem shoppingCartItem,
bool includeDiscounts,
out decimal discountAmount,
out List<DiscountForCaching> appliedDiscounts,
out int? maximumDiscountQty)
{ }
}
Base on above code I need to call both method. but call only one which plugin DependencyRegistrar Order is higher and the rest of all the methods are call from base.
Thanks.
For anyone still looking for a solution, there are multiple ways to do this.
One straight forward way this can be done -without fancy additional coding- is by using Autofac decorators.
See here about decoratorsYes it's bulky, yes it's hard to implement. Are those reasons not to use it? Of course not.
The ability to allow users the freedom to install plugins without the constant fear of breaking/losing functionality because of overridden services is much more important.
Here's an example that does not allow adding products with price 0 to shopping cart.
1. Register the decorator in your DependencyRegistrar
builder.RegisterDecorator<DShoppingCartService, IShoppingCartService>();
2. Create your service:
public class DShoppingCartService : IShoppingCartService
{
private readonly IShoppingCartService _decorated;
private readonly IDecoratorContext _context;
private readonly ILocalizationService _localizationService;
public DShoppingCartService(IShoppingCartService decorated, IDecoratorContext context, ILocalizationService localizationService)
{
this._decorated = decorated ?? throw new ArgumentNullException(nameof(decorated));
this._context = context ?? throw new ArgumentNullException(nameof(context));
_localizationService = localizationService;
}
public IList<string> AddToCart(Customer customer, Product product, ShoppingCartType shoppingCartType, int storeId, string attributesXml = null, decimal customerEnteredPrice = 0, DateTime? rentalStartDate = null, DateTime? rentalEndDate = null, int quantity = 1, bool addRequiredProducts = true)
{
if (customer == null)
throw new ArgumentNullException(nameof(customer));
if (product == null)
throw new ArgumentNullException(nameof(product));
var warnings = new List<string>();
if (shoppingCartType == ShoppingCartType.ShoppingCart && !product.CustomerEntersPrice)
{
decimal price = GetUnitPrice(product,
customer,
shoppingCartType,
1, attributesXml, customerEnteredPrice,
rentalStartDate, rentalEndDate,
true, out var _, out _);
//var finalPriceWithDiscountBase = _taxService.GetProductPrice(product, finalPrice, out var _);
//price = _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceWithDiscountBase, _workContext.WorkingCurrency);
if (price <= 0)
{
warnings.Add(_localizationService.GetResource("Products.CallForPrice"));
return warnings;
}
}
return _decorated.AddToCart(customer, product, shoppingCartType, storeId, attributesXml, customerEnteredPrice, rentalStartDate, rentalEndDate, quantity, addRequiredProducts);
}
public int DeleteExpiredShoppingCartItems(DateTime olderThanUtc)
{
return _decorated.DeleteExpiredShoppingCartItems(olderThanUtc);
}
public void DeleteShoppingCartItem(ShoppingCartItem shoppingCartItem, bool resetCheckoutData = true, bool ensureOnlyActiveCheckoutAttributes = false)
{
_decorated.DeleteShoppingCartItem(shoppingCartItem, resetCheckoutData, ensureOnlyActiveCheckoutAttributes);
}
public void DeleteShoppingCartItem(int shoppingCartItemId, bool resetCheckoutData = true, bool ensureOnlyActiveCheckoutAttributes = false)
{
_decorated.DeleteShoppingCartItem(shoppingCartItemId, resetCheckoutData, ensureOnlyActiveCheckoutAttributes);
}
public ShoppingCartItem FindShoppingCartItemInTheCart(IList<ShoppingCartItem> shoppingCart, ShoppingCartType shoppingCartType, Product product, string attributesXml = "", decimal customerEnteredPrice = 0, DateTime? rentalStartDate = null, DateTime? rentalEndDate = null)
{
return _decorated.FindShoppingCartItemInTheCart(shoppingCart, shoppingCartType, product, attributesXml, customerEnteredPrice, rentalStartDate, rentalEndDate);
}
public IEnumerable<Product> GetProductsRequiringProduct(IList<ShoppingCartItem> cart, Product product)
{
return _decorated.GetProductsRequiringProduct(cart, product);
}
public string GetRecurringCycleInfo(IList<ShoppingCartItem> shoppingCart, out int cycleLength, out RecurringProductCyclePeriod cyclePeriod, out int totalCycles)
{
return _decorated.GetRecurringCycleInfo(shoppingCart, out cycleLength, out cyclePeriod, out totalCycles);
}
public IList<string> GetRentalProductWarnings(Product product, DateTime? rentalStartDate = null, DateTime? rentalEndDate = null)
{
return _decorated.GetRentalProductWarnings(product, rentalStartDate, rentalEndDate);
}
public IList<string> GetRequiredProductWarnings(Customer customer, ShoppingCartType shoppingCartType, Product product, int storeId, int quantity, bool addRequiredProducts, int shoppingCartItemId)
{
return _decorated.GetRequiredProductWarnings(customer, shoppingCartType, product, storeId, quantity, addRequiredProducts, shoppingCartItemId);
}
public IList<ShoppingCartItem> GetShoppingCart(Customer customer, ShoppingCartType? shoppingCartType = null, int storeId = 0, int? productId = null, DateTime? createdFromUtc = null, DateTime? createdToUtc = null)
{
return _decorated.GetShoppingCart(customer, shoppingCartType, storeId, productId, createdFromUtc, createdToUtc);
}
public IList<string> GetShoppingCartItemAttributeWarnings(Customer customer, ShoppingCartType shoppingCartType, Product product, int quantity = 1, string attributesXml = "", bool ignoreNonCombinableAttributes = false, bool ignoreConditionMet = false)
{
return _decorated.GetShoppingCartItemAttributeWarnings(customer, shoppingCartType, product, quantity, attributesXml, ignoreNonCombinableAttributes, ignoreConditionMet);
}
public IList<string> GetShoppingCartItemGiftCardWarnings(ShoppingCartType shoppingCartType, Product product, string attributesXml)
{
return _decorated.GetShoppingCartItemGiftCardWarnings(shoppingCartType, product, attributesXml);
}
public IList<string> GetShoppingCartItemWarnings(Customer customer, ShoppingCartType shoppingCartType, Product product, int storeId, string attributesXml, decimal customerEnteredPrice, DateTime? rentalStartDate = null, DateTime? rentalEndDate = null, int quantity = 1, bool addRequiredProducts = true, int shoppingCartItemId = 0, bool getStandardWarnings = true, bool getAttributesWarnings = true, bool getGiftCardWarnings = true, bool getRequiredProductWarnings = true, bool getRentalWarnings = true)
{
return _decorated.GetShoppingCartItemWarnings(customer, shoppingCartType, product, storeId, attributesXml, customerEnteredPrice, rentalStartDate, rentalEndDate, quantity, addRequiredProducts, shoppingCartItemId, getStandardWarnings, getAttributesWarnings, getGiftCardWarnings, getRequiredProductWarnings, getRentalWarnings);
}
public IList<string> GetShoppingCartWarnings(IList<ShoppingCartItem> shoppingCart, string checkoutAttributesXml, bool validateCheckoutAttributes)
{
return _decorated.GetShoppingCartWarnings(shoppingCart, checkoutAttributesXml, validateCheckoutAttributes);
}
public IList<string> GetStandardWarnings(Customer customer, ShoppingCartType shoppingCartType, Product product, string attributesXml, decimal customerEnteredPrice, int quantity)
{
return _decorated.GetStandardWarnings(customer, shoppingCartType, product, attributesXml, customerEnteredPrice, quantity);
}
public decimal GetSubTotal(ShoppingCartItem shoppingCartItem, bool includeDiscounts = true)
{
return _decorated.GetSubTotal(shoppingCartItem, includeDiscounts);
}
public decimal GetSubTotal(ShoppingCartItem shoppingCartItem, bool includeDiscounts, out decimal discountAmount, out List<Discount> appliedDiscounts, out int? maximumDiscountQty)
{
return _decorated.GetSubTotal(shoppingCartItem, includeDiscounts, out discountAmount, out appliedDiscounts, out maximumDiscountQty);
}
public decimal GetUnitPrice(ShoppingCartItem shoppingCartItem, bool includeDiscounts = true)
{
return _decorated.GetUnitPrice(shoppingCartItem, includeDiscounts);
}
public decimal GetUnitPrice(ShoppingCartItem shoppingCartItem, bool includeDiscounts, out decimal discountAmount, out List<Discount> appliedDiscounts)
{
return _decorated.GetUnitPrice(shoppingCartItem, includeDiscounts, out discountAmount, out appliedDiscounts);
}
public decimal GetUnitPrice(Product product, Customer customer, ShoppingCartType shoppingCartType, int quantity, string attributesXml, decimal customerEnteredPrice, DateTime? rentalStartDate, DateTime? rentalEndDate, bool includeDiscounts, out decimal discountAmount, out List<Discount> appliedDiscounts)
{
return _decorated.GetUnitPrice(product, customer, shoppingCartType, quantity, attributesXml, customerEnteredPrice, rentalStartDate, rentalEndDate, includeDiscounts, out discountAmount, out appliedDiscounts);
}
public void MigrateShoppingCart(Customer fromCustomer, Customer toCustomer, bool includeCouponCodes)
{
_decorated.MigrateShoppingCart(fromCustomer, toCustomer, includeCouponCodes);
}
public bool ShoppingCartIsRecurring(IList<ShoppingCartItem> shoppingCart)
{
return _decorated.ShoppingCartIsRecurring(shoppingCart);
}
public bool ShoppingCartRequiresShipping(IList<ShoppingCartItem> shoppingCart)
{
return _decorated.ShoppingCartRequiresShipping(shoppingCart);
}
public IList<string> UpdateShoppingCartItem(Customer customer, int shoppingCartItemId, string attributesXml, decimal customerEnteredPrice, DateTime? rentalStartDate = null, DateTime? rentalEndDate = null, int quantity = 1, bool resetCheckoutData = true)
{
return _decorated.UpdateShoppingCartItem(customer, shoppingCartItemId, attributesXml, customerEnteredPrice, rentalStartDate, rentalEndDate, quantity, resetCheckoutData);
}
}