Plugin Override Service Virtual Method

1 年 前
Hi Mitch,


Plugin is compiled as part of nopCommerce solution and publish.

I have a dependencyRegistrar class within the plugin project

Thanks
Jon




using Arenga.Nop.Plugin.SkyHighShoes.Core.Services;
using Autofac;
using Nop.Core.Configuration;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Services.Catalog;
using System;
using System.Collections.Generic;
using System.Text;

namespace Arenga.Nop.Plugin.SkyHighShoes.Core.DependencyRegistrar
{
    public class SkyHighShoesCoreDependencyRegistrar : IDependencyRegistrar
    {
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<SkyHighShoesProductService>().As<IProductService>().InstancePerLifetimeScope();
        }

        public int Order => 1000;   // Use a high number here so we get added last and take precedence.
    }
}


1 年 前
Hi Jon,
See this forum post for the same issue.

According to this post, below solution might be worked. I not tested.

if (PluginManager.ReferencedPlugins?.SingleOrDefault(p => p.Installed && p.SystemName == "PLUGIN-NAME") == null) return;


And main thing is this issue is recently fixed by nopCommerce team.
Please go through this post, there are several commits in current working develop branch. You can check and customize code and fix in your current version.
1 年 前
You can also check plugin is installed on starting off your overridden service code and check your plugin is installed or not.

It it not installed then call base.MAINSERVICEMETHODNAME().

This will be working fine.
1 年 前
Many thanks for your insight raju. I will check out these approaches. Much appreciated.

Jon
9 个月 前
Hi All. I am trying to override a method of a OrderTotalCalculationService service with a new plugin. But i Can't hit the new plugin method. My New plugin is already installed and enable.

Here my Code:

DependecyRegistrar.cs

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

            builder.RegisterType<BlockProductDiscountService>().As<IBlockProductDiscountService>().InstancePerLifetimeScope();
          
        public int Order
        {
            get
            {
                return 10;
            }
        }


NewServiceMyPlugin.cs

    public class NewServiceMyPlugin : OrderTotalCalculationService
    {

        #region Fields
FIELDS ZONE
      
        #endregion

        #region Ctor

CONSTRUCTOR ZONE

        #endregion

        public override void GetShoppingCartSubTotal(IList<ShoppingCartItem> cart, bool includingTax,
        out decimal discountAmount, out List<DiscountForCaching> appliedDiscounts,
        out decimal subTotalWithoutDiscount, out decimal subTotalWithDiscount,
        out SortedDictionary<decimal, decimal> taxRates)
        {
          All the code Here, I Can't hit this method.
        }

Any suggestion?. Thanks in advance.
9 个月 前
Add this line in Dependency registrar

builder.RegisterType<NewServiceMyPlugin>().As<IOrderTotalCalculationService>().InstancePerLifetimeScope();
9 个月 前
Thanks for your answer.. It's working now
9 个月 前
OK my plugin works perfect on my local Dev. But when I'm trying to deploy it on Iss I get a error Msg and the page Crash. pls click on the link: Error MSG

This is my MyNewMethodPlugin.cs
using Nop.Core.Plugins;
using Nop.Services.Common;

namespace Nop.Plugin.Misc.MyNewMethod
{
    public class MyNewMethodPlugin: BasePlugin, IMiscPlugin
    {
        public override void Install()
        {
            base.Install();
        }

        public override void Uninstall()
        {

            base.Uninstall();
        }
    }
}


My DependencyRegistrar.cs

using Autofac;
using Nop.Core.Configuration;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Plugin.Misc.CRBlockProductDiscount.Services;


namespace Nop.Plugin.Misc.MyNewMethod
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<MyNewMethodPluginService>().As<Nop.Services.Orders.IOrderTotalCalculationService>().InstancePerLifetimeScope();
        }
        public int Order
        {
            get
            {
                return 10;
            }
        }
    }
}


My MyNewMethodPluginService.cs


using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Discounts;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Shipping;
using Nop.Core.Domain.Tax;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.Discounts;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Services.Shipping;
using Nop.Services.Tax;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Plugin.Misc.MyNewMethod.Services
{
    public class MyNewMethodPluginService: OrderTotalCalculationService
    {

        #region Fields

        private readonly IStoreContext _storeContext;
        private readonly IPriceCalculationService _priceCalculationService;
        private readonly ITaxService _taxService;
        private readonly ICheckoutAttributeParser _checkoutAttributeParser;
        private readonly IDiscountService _discountService;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly ShoppingCartSettings _shoppingCartSettings;
        private readonly CatalogSettings _catalogSettings;


        #endregion

        #region Ctor

        public MyNewMethodPluginService(IWorkContext workContext,
            IStoreContext storeContext,
            IPriceCalculationService priceCalculationService,
            IProductService productService,
            IProductAttributeParser productAttributeParser,
            ITaxService taxService,
            IShippingService shippingService,
            IPaymentService paymentService,
            ICheckoutAttributeParser checkoutAttributeParser,
            IDiscountService discountService,
            IGiftCardService giftCardService,
            IGenericAttributeService genericAttributeService,
            IRewardPointService rewardPointService,
            TaxSettings taxSettings,
            RewardPointsSettings rewardPointsSettings,
            ShippingSettings shippingSettings,
            ShoppingCartSettings shoppingCartSettings,
            CatalogSettings catalogSettings)
            : base(workContext,
                 storeContext,
                 priceCalculationService,
                 productService,
                 productAttributeParser,
                 taxService,
                 shippingService,
                 paymentService,
                 checkoutAttributeParser,
                 discountService,
                 giftCardService,
                 genericAttributeService,
                 rewardPointService,
                 taxSettings,
                 rewardPointsSettings,
                 shippingSettings,
                 shoppingCartSettings,
                 catalogSettings
            )
        {

            this._storeContext = storeContext;
            this._priceCalculationService = priceCalculationService;
            this._taxService = taxService;
            this._checkoutAttributeParser = checkoutAttributeParser;
            this._discountService = discountService;
            this._genericAttributeService = genericAttributeService;
            this._shoppingCartSettings = shoppingCartSettings;
            this._catalogSettings = catalogSettings;
        }

        #endregion

        #region Methods
        public override void GetShoppingCartSubTotal(IList<ShoppingCartItem> cart, bool includingTax,
        out decimal discountAmount, out List<DiscountForCaching> appliedDiscounts,
        out decimal subTotalWithoutDiscount, out decimal subTotalWithDiscount,
        out SortedDictionary<decimal, decimal> taxRates)

        {

MY BUSINESS RULES HERE
}
}

and my plugin.json

{
  "Group": "Misc",
  "FriendlyName": "Block Products with Non-Discountable Attribute",
  "SystemName": "Misc.MyNewMethod",
  "Version": "1.0",
  "SupportedVersions": [ "4.00" ],
  "Author": "ME",
  "DisplayOrder": 1,
  "FileName": "Nop.Plugin.Misc.MyNewMethod.dll",
  "Description": "This plugin use the NonDiscountable attribute to the product for any discount"
}


Thanks in advance guys!
3 个月 前
https://drive.google.com/file/d/1rC-l7Dxw2aVe9GJLW5FHQI9wu-0eL0Wj/viewa.m. wrote:
Sure
1. Create your custom new class derived from OrderProcessingService
2. Override the CheckOrderStatus  method
3. Create a new class implementing from IDependencyRegistrar. Set its "Order" property to some big value so it's run after the default one
4. Register your new custom class as IOrderProcessingService.

This way your implementation will be used instead of the default one


Hello there. I can't solve this problem. Can you help me?
1 个月 前
kamranhuseyns wrote:
Hello there. I can't solve this problem. Can you help me?

Did you work out the issue ?

Something has happened to my code and my overrides no longer work
Code that has not changed and works perfectly in v4.2 no longer works in v4.3

For a test I went back to a total clean version of v4.3 add the code as detailed here
a.m. wrote:
1. Create your custom new class derived from OrderProcessingService
2. Override the MoveShoppingCartItemsToOrderItems method
3. Create a new class implementing from IDependencyRegistrar. Set its "Order" property to some big value so it's run after the default one
4. Register your new custom class as IOrderProcessingService.

I even went back to an Visual Studio v16.5.3 to see if that fixed it but no !
Because it is making me crazy I made a test case https://dotnetfiddle.net/srlurM
It does not work either ????