Override Features Via Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi All,

I'm new to nopCommerce and quite new to MVC. I'd like to make a number of bespoke changes to the system without making changes to the core framework, allowing me to update to the latest version without loasing all my changes.

I've been looking through and I figure that doing my changes through a custom plugin would probably be the best solution.

I've got my plugin setup but I'm looking for some guidance on how I can modify and override functionality from the confinds of my plugin.

Some things I would like to do include:

- Override the homepage meta title and description.
- Add a canonical tag to the homepage

Can someone point me in the right direction to do the above from my plugin? Does anyone know of any good resources for extending the nopCommerce functionality using plugins?
12 years ago
I'd really love to know more about this too. I have been making changes to the core code but I need to spend some time researching plugins and how best to approach it - any advice (or sample code) will be greatly appreciated.
12 years ago
Scrooby wrote:
Hi All,

I'm new to nopCommerce and quite new to MVC. I'd like to make a number of bespoke changes to the system without making changes to the core framework, allowing me to update to the latest version without loasing all my changes.

I've been looking through and I figure that doing my changes through a custom plugin would probably be the best solution.

I've got my plugin setup but I'm looking for some guidance on how I can modify and override functionality from the confinds of my plugin.

Some things I would like to do include:

- Override the homepage meta title and description.
- Add a canonical tag to the homepage

Can someone point me in the right direction to do the above from my plugin? Does anyone know of any good resources for extending the nopCommerce functionality using plugins?



One possibility is to create a new plugin and copy the core home controller and core home views into your plugin. Rename the controller and views folder "ScroobyHomeController" and "ScroobyHome". Then create a new route provider in your plugin and programmatically remove the "HomePage" route and add your own implementation of it  that looks something like


            routes.MapLocalizedRoute("HomePage",
                            "",
                            new { controller = "ScroobyHome", action = "Index"},
                            new[] { "Nop.Plugin.Scrooby.HomePage.Controllers" });




http://blog.csharpwebdeveloper.com/2011/09/27/cheat-sheet-for-effectively-extending-nopcommerce/
12 years ago
Hi Skyler, thanks for your response.

Ideally I wouldn't want to copy the core controllers because when an update is released, I would have to merge my changes with the new controller. Surely that would eliminate the point of copying the controllers, I may as well just edit the original files?

Is there a way to override the controllers?

I've created my own HomeController within my plugin and set it to inherit the core HomeController. As you suggested, I created my own route for the homepage. However, because the methods in the core HomeController aren't virtual, I can't override them.

I also tried the same with the ShoppingCartController, but because some methods and properties are private, I can't access them.

Is there a way to override the controllers like I'm trying at the moment? Or am I going about this completely wrong?
12 years ago
Scrooby wrote:
Hi Skyler, thanks for your response.

Ideally I wouldn't want to copy the core controllers because when an update is released, I would have to merge my changes with the new controller. Surely that would eliminate the point of copying the controllers, I may as well just edit the original files?

Is there a way to override the controllers?

I've created my own HomeController within my plugin and set it to inherit the core HomeController. As you suggested, I created my own route for the homepage. However, because the methods in the core HomeController aren't virtual, I can't override them.

I also tried the same with the ShoppingCartController, but because some methods and properties are private, I can't access them.

Is there a way to override the controllers like I'm trying at the moment? Or am I going about this completely wrong?


Unfortunately your options are to either make the change I recommended or modify the file directly without the use of a plugin. Few of the controllers in nopCommerce (and most MVC projects) can be subclassed successfully.
12 years ago
I've been making the controller partial classes and then adding them to my themes folder.. The only change to the core is adding the partial and the rest is done in my controller class..   Re-adding partial to the classes is a simple thing on an upgrade..
11 years ago
pictoric wrote:
I've been making the controller partial classes and then adding them to my themes folder.. The only change to the core is adding the partial and the rest is done in my controller class..   Re-adding partial to the classes is a simple thing on an upgrade..


Hi Pictoric,

I'm interested in following your approach to extend the controllers.  Do you mind providing an example?  I would like to know a little bit more in detail how are you organizing the classes in your theme folder.

Thanks.
11 years ago
I also like this approach. An example would be great.
11 years ago
The C# documentation about partial classes

at microsoft msdn

says that a class can be divided in several files in the same namespace

Therefor if you want to amplify for example productvariant by some columns
you just add your part to a new partial class and add the additional classfile to the nop.core folder at specific namespacepoint

e.g. for productvariant: nop.core.domain.catalog
11 years ago
pictoric wrote:
I've been making the controller partial classes and then adding them to my themes folder.. The only change to the core is adding the partial and the rest is done in my controller class..   Re-adding partial to the classes is a simple thing on an upgrade..


One disadvantage is that you must recompile the Nop.Web project for it to work and your source code is also available. This is because the .NET Framework doesn't allow for partial classes to exist in another project (it would get confusing if it was allowed). If your plugin contains proprietary code then copying the controller and modifying it in a maintainable way (creating a region with controller customizations for example) seems to be a good approach. I'm also interested in a better way to extend nopCommerce via plugins without having to recompile or merge controllers/services.

Ideally, clicking Install should be the only action a user should need to perform for installing a plugin. The plugin should do the rest (e.g. copying files to the necessary folders, adding locale resource strings, etc). Furthermore, Themes should be as easy as dropping a folder and/or installing one associated theme plugin.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.