Plugin Help

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
First off, I'm new to nopCommerce but I like what I see so far and have had a lot of success with getting up to this point.  Half way through this project I decided to upgrade to the latest 2.1 version and, so far am really impressed by the new syntax.

I'm trying to create a separation of a couple of controls which are used on the Single Variant and Variant in Grid templates.  Specifically I am trying to have a different related products control for the single variant and variant in grid templates.  It's mostly for aesthetic reasons at this time but there may be other reasons for the separation in the future.  

My solution for this problem at this point was very direct but I believe it will cause me problems in the future.  

My solution:
in the CatalogController.cs file add the following code for a second razor page;

        [ChildActionOnly]
        public ActionResult RelatedProductsNEW(int productId)
        {
            var product = _productService.GetProductById(productId);
            if (product == null)
                throw new ArgumentException("No product found with the specified id");
            
            var products = _productService.SearchProducts(0,
                0, null, null, null, productId, 0, null, false,
                0, null, ProductSortingEnum.Position, 0, int.MaxValue);

            var model = products.Select(x => PrepareProductOverviewModel(x)).ToList();

            return PartialView(model);
        }

Then I copied the RelatedProducts.cshtml file in Views/Catalog and named RelatedProductsNEW.cshtml.  Point the Variants in Grid template to the RelatedProductsNew control and bingo!!!

Recompiled and it created a new nop.web.dll which ran perfectly.  

Only problem.  Upgrades...

When a new release comes out I'm now going to have to re-add my code and recompile every time.  Also I would like to be able to apply this to other projects in the future without having to re-do it every time.
Is there a way to accomplish the same thing but have it compile a separate dll which I can install using your plugin architecture?

I know it's a rookie question. I am definitely still learning.  

I've also added quite a few changes to the existing razor pages which I am unsure if I should have done directly or in new pages.  I created a different shared template from _2ColumnRoot.cshtml but other than that I have made my changes directly in the default razor pages.  Again I'm concerned about when I upgrade, will I have to re-integrate my changes back in?
I've looked through the forum quite a bit and found some great tutorials, but I'm not sure in this case which direction I should go.
Thank you in advance for your help.
12 years ago
eric.shafer wrote:
First off, I'm new to nopCommerce but I like what I see so far and have had a lot of success with getting up to this point.  Half way through this project I decided to upgrade to the latest 2.1 version and, so far am really impressed by the new syntax.

I'm trying to create a separation of a couple of controls which are used on the Single Variant and Variant in Grid templates.  Specifically I am trying to have a different related products control for the single variant and variant in grid templates.  It's mostly for aesthetic reasons at this time but there may be other reasons for the separation in the future.  

My solution for this problem at this point was very direct but I believe it will cause me problems in the future.  

My solution:
in the CatalogController.cs file add the following code for a second razor page;

        [ChildActionOnly]
        public ActionResult RelatedProductsNEW(int productId)
        {
            var product = _productService.GetProductById(productId);
            if (product == null)
                throw new ArgumentException("No product found with the specified id");
            
            var products = _productService.SearchProducts(0,
                0, null, null, null, productId, 0, null, false,
                0, null, ProductSortingEnum.Position, 0, int.MaxValue);

            var model = products.Select(x => PrepareProductOverviewModel(x)).ToList();

            return PartialView(model);
        }

Then I copied the RelatedProducts.cshtml file in Views/Catalog and named RelatedProductsNEW.cshtml.  Point the Variants in Grid template to the RelatedProductsNew control and bingo!!!

Recompiled and it created a new nop.web.dll which ran perfectly.  

Only problem.  Upgrades...

When a new release comes out I'm now going to have to re-add my code and recompile every time.  Also I would like to be able to apply this to other projects in the future without having to re-do it every time.
Is there a way to accomplish the same thing but have it compile a separate dll which I can install using your plugin architecture?

I know it's a rookie question. I am definitely still learning.  

I've also added quite a few changes to the existing razor pages which I am unsure if I should have done directly or in new pages.  I created a different shared template from _2ColumnRoot.cshtml but other than that I have made my changes directly in the default razor pages.  Again I'm concerned about when I upgrade, will I have to re-integrate my changes back in?
I've looked through the forum quite a bit and found some great tutorials, but I'm not sure in this case which direction I should go.
Thank you in advance for your help.


Great question Eric! You have a couple of options for making "upgrade-resistant" modifications. First there is a hierarchy when loading views. It will try and load from theme folders before it loads from the main views. If you create a new view and start overriding views you can take updates and be resistant to the change during upgrades.

Creating new objects (controllers, views, models, etc.) Is usually pretty safe and would only require merging project files. Creating new libraries (plugin architecture or not) is the most resistant and would only require merging the solution file and Global.asax.cs at most.

When writing code do your absolute best to follow the Open/Closed principle. Make use of C# features like partial classes and extension methods to avoid changing nopCommerce code and instead make extensions off of the code.

I think the question you've presented above would be best solved by a new controller and overtime it might evolve into something bigger, but for now a new controller would work best. When an update is released you will have to re-add the new files/folders back to the project, but it is a small price.

Alternatively you could take a much more advanced approach by cloning the source code from the codeplex nopCommerce 2.1version and work off of that (committing your changes to your local copy). Then when 2.2 is released "pull" from codeplex and merge differences using tortoisehg or your preferred diff tool.

Basically, it is going to come down to preference and each scenario will need to be considered separately. Different types of changes require different levels of attention. Good luck!
12 years ago
eric.shafer wrote:

I've also added quite a few changes to the existing razor pages which I am unsure if I should have done directly or in new pages.  I created a different shared template from _2ColumnRoot.cshtml but other than that I have made my changes directly in the default razor pages.  Again I'm concerned about when I upgrade, will I have to re-integrate my changes back in?


Maybe you should take a look at this post about Asoares trick:
https://www.nopcommerce.com/boards/t/11830/instructing-to-load-_rootcshtml-from-theme-viewsshared-folder.aspx
12 years ago
Thank you both a ton.  Both of your answers have completely changed the way I will set up my site but now I am confident I can do it right so it will stand up to upgrades.  

I'm spending the next day or so moving all my changes out into the theme folder and resetting the default views as well as implementing Asoares trick.

Thank you very much for your quick response to my post.  Hopefully your answers will help others as well.
Keep up the good work.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.