Overriding NopCommerce Controller Methods

This relies on the Custom View Engine and Route Provider implementation from the Implementing a Base NopCommerce Plugin tutorial.

  1. Locate the controller that contains the code that you want to override in NopCommerce
  2. Create a controller in your plugin's controller directory named the same as the controller in the presentation project, ex. Controllers/CommonController.cs
  3. Dependency inject whichever services or settings you need.
  4. write the method and add your logic inside.
[ChildActionOnly]
public virtual ActionResult AdminHeaderLinks()
{
    var x = DoNewStuff();
    var model = GetViewModel(x);

    return PartialView(model);
}

If your Controller method has arguments or is not overriding / firing, you may need to manually override the route in the Route Provider

  • We create the route with a URL parameter that takes your argument
  • We remove the existing route that matches the URL's pattern
  • We add back in the route from our plugin's controller.

below is an example for overriding the GetDownload method in the DownloadController in Nop.Web

public void RegisterRoutes(RouteCollection routes)
{
    ViewEngines.Engines.Insert(0, new CustomViewEngine());

    var route = routes.MapLocalizedRoute("Plugin.Misc.DigitalContent.GetDownload",
        "Download/GetDownload/{orderItemId}",
        new { Controller = "Download", action = "GetDownload" },
        new[] { "Nop.Plugin.Misc.DigitalContent.Controllers" }
    );
    routes.Remove(route);
    routes.Insert(0, route);
}

Documentation and Notes by Lee Jones Oct-2017

results matching ""

    No results matching ""