Getting instance of IOrderTotalCalculationService from IDiscountRequirementRule

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I'm trying to create a discount requirement rule that is based on the shopping cart subtotal.

I was hoping to be able to inject the dependency on IOrderTotalCalculationService into my implementation of IDiscountRequirementRule by adding an argument of type IOrderTotalCalculationService in the ctor, but the DependencyResolver doesn't appear to resolving the dependencies when the discount requirement rule object is instantiated.

I tried cheating by adding this line of code to the CheckRequirement() method of my rule:

var service = DependencyResolver.Current.GetService(typeof(IOrderTotalCalculationService)) as IOrderTotalCalculationService;


However, this doesn't seem to be working either. Any help would be appreciated.
12 years ago
I think it's because that interface is for plugins and has to be loaded differently.  I don't have time to investigate further but it might be similar to how the TaxService loads the active tax provider.
12 years ago
It should work fine. Have a look at \Plugins\Nop.Plugin.DiscountRules.PurchasedAllProducts\PurchasedAllProductsDiscountRequirementRule.cs (IOrderService is injected using ctor)
12 years ago
a.m. wrote:
It should work fine. Have a look at \Plugins\Nop.Plugin.DiscountRules.PurchasedAllProducts\PurchasedAllProductsDiscountRequirementRule.cs (IOrderService is injected using ctor)


Well, I'll try injecting into the ctor again, but shouldn't calling DependencyResolver.Current.GetService() directly inject all the dependencies of the type being instantiated as well?

I'm really puzzled why neither method worked the first time. Just to make sure that I configured my plugin project correctly, I hardcoded CheckRequirement() to randomly return true or false without using IOrderTotalCalculationService at all, and the rule seemed to work fine.

As soon as I tried to get an instance of IOrderTotalCalculationService either through injection or direct instantiation through the DependencyResolver, I get a crash.
12 years ago
I figured out the problem. Calling IOrderTotalCalculationService.GetShoppingCartSubTotal() from within IDiscountRequirementRule.CheckRequirement() causes an infinite recursion because IOrderTotalCalculationService.GetShoppingCartSubTotal() includes discounts in the calculation.

Unfortunately, there isn't an argument that you can pass to IOrderTotalCalculationService.GetShoppingCartSubTotal() to turn off inclusion of discounts.

I ended up having to use IPriceCalculationService instead like this:

var cart = request.Customer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();
var subtotal = cart.Sum(x => _priceCalculationService.GetSubTotal(x, false));
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.