This article transferred from: http://www.cnblogs.com/ganqiyin/p/3680771.html
Original site:http://www.nopcommerce.com/docs/77/how-to-write-a-nopcommerce-plugin.aspx
Plug-in (or plugin) is a component that adds specific capabilities to larger software applications (Wikipedia)
Plugins are used to extend the Nopcommerce functionality. Nopcommerce has several types of plugins. Examples: Payment methods (PAYPAL), tax authorities, method of delivery calculation (UPS, USP, FedEx), widgets ( such as "Live chat" blocks), etc. Nopcommerce itself comes with a lot of different plugins. You can also search for a variety of plugins that meet your requirements on the Nopcommerce official website. If not, this article will walk you through the process of creating your own plug-in.
The plug-in structure, the required files and where
1 . The first thing you need to do is create a new class library project in the solution. It is a good practice to put all the plugins in the \ Plugins directory at the root of your solution (not to be confused with \ plugins subdirectories in the Nop.web project . Because this is where the plugin DLL file for the published build is placed). This is a good practice to put all the plugins into the "Plugins" solution folder (for more information about the solution folder, click here).
It's best to name it this way: "Nop.plugin." {Group}. {Name} '. {Group} is your plugin's classification (such as payment), {name} is your plug-in name (such as "AuthorizeNet"), then Authorize.Net's payment plugin will have this name Word: Nop.Plugin.Payments.AuthorizeNet.
2, after creating the project, we need to set the output location of the project compile file DLL: ". \.. \presentation\nop.web\plugins\{group}. {name}\ ". For example, the Authorize.Net payment plugin will have such an input path: ". \.. \presentation\nop.web\plugins\payments.authorizenet\ ". After the setup is complete, the corresponding plug-in DLL compiles the output to the \presentation\nop.web\plugins\ folder, and the Nopcommerce kernel searches for the folder.
Method: Select Item-"Right mouse click-" Select Generate-"Click the browse button to set the path (both in debug and release mode)
3, next you need to create a Description.txt for each of your plugins, this file contains information that describes the plugin. You can copy it from the other plugins directory. For example, Authorize.Net payment plug-in Description.txt has the following content:
Group:payment Methodsfriendlyname:credit cardsystemname:payments.authorizenetversion:1.00supportedversions: 2.30author:nopcommerce Teamdisplayorder:1filename:nop.plugin.payments.authorizenet.dll
4.The final step is to create a class that implements the IPlugin (Nop.Core.Plugins namespace) of the interface. Nopcommerce Baseplugin class has implemented some IPlugin methods, you can not need to implement it again, so that the function code to avoid duplication. Nopcommerce also provides some specific interfaces derived from the IPlugin.For example, to create a new payment method plug-in "Ipaymentmethod" interface. it contains a number of methods specific to specific payment functions, such as processpayment () or Getadditionalhandlingfee (). currently Nopcommerce has the following specific plug-in interfaces:
- iexternalauthenticationmethod: used to create external authentication methods, such as Facebook,twitter, OpenID, etc.
- iwidgetplugin: It allows you to create widgets. widgets are rendered in certain parts of your site. For example, it can display an "online chat" block in the left column of your site.
- Iexchangerateprovider: used to get the currency exchange rate.
- idiscountrequirementrule: allows you to create new discount rules, such as "the country to which the bill is sent must be ..."
- Ipaymentmethod: is the plug-in interface for payment processing.
- Ishippingratecomputationmethod: is used to obtain the available shipping method and the correct shipping cost. For example, Ups,ups,fedex and so on.
- itaxprovider: tax provider used to obtain tax rates
If these interfaces are not suitable for your plug-in, you can use the "Imiscplugin" interface.
Note: After you complete the project build, clean up the solution because some resources are cached and may be problematic when developers continue coding.
Processing requests (requests). Controller (Controllers), model (models), and view (views).
Now you can see our plugin in admin area > Configuration > Plugins. But as you can guess, our plugin does nothing. You don't even need to configure the user interface for it. Now let's Lyle create a plugin configuration page. All we need to do is create a controller, model and view.
- The MVC Controller is responsible for responding to requests made by an ASP. NET MVC Web site. Each browser request is mapped to a specific controller.
- A view contains the HTML tags and content that are sent to the browser. A view is a page that is equivalent to an ASP. NET MVC application.
- An MVC model contains all application logic other than the view or controller.
About the MVC pattern here you can find more information.
Now we can start to write the plugin:
- Create a model: Add a folder named "Models" to the new plug-in project and add a model class that you need in the folder.
- Create a View : Add a folder named "Views" in the new plug-in project and add a {name} folder to the folder, where {name} refers to your plug-in name. Then add a configure.cshtml file. Note: This view should indicate that it is an embedded resource.
- Create a controller: add a folder named "Controllers" to the new plug-in project, and then add a controller class to the folder. The best way to name it is {Group}{name}controller.cs. For example, Paymentauthorizenetcontroller. Then create a method called "Configure" in the controller for configuration. Prepare a model class and pass it to this view: "Nop.plugin. {Group}. {Name}. Views. {Group} {Name}. Configure "(that is, the embedded view). For example, if you look at the Paymentauthorizenetcontroller implementation in the Authorize.Net payment plugin, you'll be more clear.
Tip 1: open Any other plugins and copy the Web. config from it to your plug-in project. This file allows you to have IntelliSense when making a view. IntelliSense is the automatic completion of Microsoft implementations.
Tip 2: The simplest way to complete the above steps is to open any other plugin and copy its files to your plugin project. Then, just rename the classes and folders is OK.
Tip 3: If you want to restrict how the Administrator (shopkeeper) can access the controller, then it is OK to mark the method with the [Adminauthorize] attribute.
Tip 4: Finally, make sure that the Copy Local property of all third-party assembly references is set to "False" (do not replicate). This will reduce the size of the deployed files.
Routing
Now let's register the appropriate route for the plugin. Asp. NET route is used to map requests sent by the browser into the appropriate action method of the MVC controller, where you can find more information about the route. Please follow the steps below to register:
1, some close-up plug-in interface (as described above) and "Imiscplugin" interface has a "Getconfigurationroute" method. It should return a route to the controller for the background configuration of the plug-in. Implement your plugin's "Getconfigurationroute" method to tell Nopcommerce what your plugin's background routing configuration is. If your plugin does not have a background configuration, then this method will return null, such as the following:
1Publicvoid Getconfigurationroute (OutStringActionName,2OutStringControllername,3OutRouteValueDictionary routevalues)4{5 ActionName ="Configure";6 controllername ="Paymentauthorizenet"new 8 namespaces ", " nop.plugin.payments.authorizenet.controllers< Span style= "color: #800000;" > "", 10 {areanull}11 }; 12}
Sample Code
2. (optional) If you need to add some custom routes, you can create a RouteProvider.cs file. It informs the Nopcommerce system about the routing of the plug-in. For example, the following Routeprovider class adds a new route that can be opened by opening a Web browser and navigating to the http://www.yourStore.com/Plugins/PaymentPayPalStandard/ Pdthandler URL (using the PayPal plugin) to access a new route:
1PublicPartialClassRouteprovider:irouteprovider2{3PublicvoidRegisterRoutes (routecollection routes)4{567 routes. MapRoute ("Plugin.Payments.PayPalStandard.PDTHandler",8"Plugins/paymentpaypalstandard/pdthandler",9New {controller ="Paymentpaypalstandard", action ="Pdthandler"},10New[] {"Nop.Plugin.Payments.PayPalStandard.Controllers"}11 ); 12 13 }14 public int priority 15 {16 get17 {18 return 0;}20 }21}
Sample Code
after the plugin is installed and the configuration method is added, you can find a configuration link in Admin > Configuration > Plugins.
Handling the install and unload methods
This step is optional. Some plugins require additional logic during the installation process. For example, a plug-in can add local resources. Create a new class that implements the IPlugin interface (in most cases, derive from the Baseplugin Class) and override the following method:
- Install. This method is called during the plug-in installation process. You can initialize any settings here, add local resources, or create some new database tables, if necessary.
- Uninstall. This method cancels the plug-in invocation.
Note: If you override one of these methods, you need to call the base method and not hide it. For example, to rewrite the "install" method, you need to call the following method "Base.install ()", the Authorize.Net Plugin's "Install" method looks like the following code:
1PublicOverridevoidInstall ()2{3var settings =NewAuthorizenetpaymentsettings ()4{5 Usesandbox =true, 6 Transactmode =< Span style= "color: #000000;" > Transactmode.authorize," 123 ", 8 LoginId = "456 9 }; 10 _settingservice.savesetting (settings); 11 12 base13}
Install method Override Example
Note: installed plugins can be found in "\ app_data\ InstalledPlugins.txt". This checklist was created during the installation process.
Upgrading Nopcommerce may not work for plugins
Some plugins may not be compatible with Nopcommerce new version after Nopcommerce upgrade. If there is a problem after upgrading to a newer version, remove the plugin and then on the Nopcommerce official website, see if there is a plugin compatible with Nopcommerce new version. Most plug-in developers will upgrade their plugins to accommodate the new version, but some plugins will not be upgraded to be incompatible with the new version of Nopcommerce. In most cases, however, you can open the appropriate Description.txt file and edit the supportedversions field.
Summary
Hopefully this article will allow you to start nopcommerce the plugin journey and develop a great plugin.
[Turn]nopcommerce Mall system--how to write a plugin