Overriding the controllers

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 лет назад
Hi,

I've managed to override the ShoppingCartController with a  plugin, but I have had to modify the core, because overriding the procedure was causing an error - I did a simple modification by adding "virtual" to the method I needed to override :

I had to modify the original ShoppingCartController.cs in Nop.Web


        [NonAction]
        protected virtual void PrepareShoppingCartModel(ShoppingCartModel model,


and now in the plugin I can use


        [NonAction]
        protected override void PrepareShoppingCartModel(ShoppingCartModel model,


This is the first override I've done on a controller as it is quite a difficult, but it could be easier. I've seen in the change logs about adding "virtual" - but as this is the first time I've had to modify the core I wanted to ask if this "virtual" attribute will be added to the controllers for future versions of nop, so that overriding controllers via plugins is easier. For a previous plugin I had to copy the controller, create a duplicate and override the routes - but this method allows me to override just what I need, so upgrades will be easier to maintain.

thanks,
10 лет назад
Just curious why you didn't just copy the method(s) into your plugin and override the "/cart" route map(s) to point to your own controller...

no core mods required.

I guess at the end of the post you allude to this approach, but you don't have to override all the routes... only the ones you need.
10 лет назад
I agree.  I think any core method that is public should be virtual for extending it.
10 лет назад
in my plugin i also had to extend the shoppingcartcontroller and to accomplish without modifying the nop code i did it in the same way as mattsoundworld said.

it works very well, but it doesn't feel fine to copy the source in your own plugin, i also think that all public controller methods should be at least virtual
10 лет назад
Basically - It's a customization to allow no VAT for certain size variations (UK, child sizes are VAT Free!) I don't need to override any of the Actions for this plugin - only the [NonActions] - the plugin has overrides for:


CatalogController.cs
OrderController.cs
OrderTotalCalculationService.cs
PriceCalculationService.cs
ProductAttributeFormatter.cs
ShoppingCart.cs
ShoppingCartControllercs
TaxService.cs

Adding "virtual" to the functions I need to override means that I have to use a custom Nop.Services.dll and Nop.Web.dll - but this isn't an issue for this project as I've documented the process and hopefully when we upgrade to 3.3 all the protected [NonAction] functions in all the controllers will have "virtual" added - I will put a request in the 3.3 thread for this to see if it's possible.

Thanks for the replies...
10 лет назад
Thanks for suggestions. Please see work item here
9 лет назад
Upgrading the store in question from 3.1 to 3.3 has highlighted that I still need to modify the core to give the functionality I need. I have now voted for the workitem (bit late I know ;-) as we have some more stores being developed that need the zero VAT for child sizes, so I hope that the plugin architecture can be further developed for future versions of Nop.

Is there a way that I can contribute to the plugin architecture? There are a few more points in the code where dummy classes could be added, specifically for adding routines from plugins - one that comes to mind would be in the Global.asax Application_Begin_Request.
9 лет назад
I've finished the upgrade and I can see that some of the proposed work has been completed, but I still have a custom Nop.Admin.Dll and Nop.Web.Dll because "virtual" hasn't added to the [NonAction]'s in the controllers; specifically:

nop.admin - OrderController, line 228, protected virtual void PrepareOrderDetailsModel(OrderModel model, Order order)

nop.web - CatalogController, line 313,         protected virtual IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products,
            bool preparePriceModel = true, bool preparePictureModel = true,
            int? productThumbPictureSize = null, bool prepareSpecificationAttributes = false,
            bool forceRedirectionAfterAddingToCart = false)

nop.web OrderController, line 98, protected virtual OrderDetailsModel PrepareOrderDetailsModel(Order order)

nop.web ShoppingCartContoller, line 243,         protected virtual void PrepareShoppingCartModel(ShoppingCartModel model,
            IList<ShoppingCartItem> cart, bool isEditable = true,
            bool validateCheckoutAttributes = false,
            bool prepareEstimateShippingIfEnabled = true, bool setEstimateShippingDefaultAddress = true,
            bool prepareAndDisplayOrderReviewData = false)


These are the points in the code that actually render the price, so I'm doing a simple flip-flop on the tax.
9 лет назад
marc wrote:
Is there a way that I can contribute to the plugin architecture?

Sure. Please find more info here. Read "Contributing source code (Git SCM version for developers)".

marc wrote:
...but I still have a custom Nop.Admin.Dll and Nop.Web.Dll because "virtual" hasn't added to the [NonAction]'s in the controllers; specifically....

Done. Please see changeset f3a300be8f51. Thanks
9 лет назад
Awesome!!!!!!!!!!!!!!!!!!!!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.