To write the plugin follow this guide: https://www.nopcommerce.com/docs/77/how-to-write-a-nopcommerce-plugin.aspx
When you have your plugin setup, look at the admin ProductsController's POST-method "Create", to see how you can add products. Presentation/Nop.Admin/Controllers/ProductController.cs Line: 612
If you want to add more than just the product itself (prices, images, meta etc), you can take a look at ImportManager.cs Libraries/Nop.Services/ExportImport/ImportManager.cs Line: 53
thanks for the reply, as i am newbie so i am having trouble in writing plugins based on MVC? i have read the differnet articles "how to write plugins" but i cant find any simple example. would you please specify here with some code and steps how can i write a plugin which adds products?
I don't think you can find a better/simpler explanation on how to build a nopCommerce plugin than that. What part of the article are you having trouble understand? It indeed demands basic knowledge of ASP.NET MVC.
The Controller part, i am confused about using it in order to add product to nopCommerce database. Can you just place a snippet here for adding a product ? also in MVC we have a model, do i have to add a Product model too? i can directly use the Product class if any from nopCommerce library.
This is my Controller looks like, please guide me this is correct. Also tell me if i am missing something to add product? how can i add product to particular category?
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using Nop.Admin.Models.Catalog; using Nop.Core.Infrastructure; using Nop.Plugin.Misc.ProductImport.Models; using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.Product.Controllers { class ProductImportController : Controller { // // GET: /Default1/ public void CreateProduct() { var productService = new NopEngine().Resolve<IProductService>();
var prod = new Core.Domain.Catalog.Product { Name = "Test product", UpdatedOnUtc = DateTime.UtcNow, CreatedOnUtc = DateTime.UtcNow, };
Many thanks i really appreciate your help , ok now another thing i have MS SQL database which has a table of categories and Products, i want to fetch it from that DB and insert into nopCommerce DB, how can i achieve this?
My Controller look like this now.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using Nop.Admin.Models.Catalog; using Nop.Core.Domain.Catalog; using Nop.Core.Infrastructure; using Nop.Plugin.Misc.ProductImport.Models; using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.Product.Controllers { class ProductImportController : Controller { // // GET: /Default1/ public void CreateProduct() { //product service var _productService = new NopEngine().Resolve<IProductService>();
// category service var _categoryService = new NopEngine().Resolve<ICategoryService>();
//declare product instance Core.Domain.Catalog.Product prod = new Core.Domain.Catalog.Product() { Name = "DELL 4530", UpdatedOnUtc = DateTime.UtcNow, CreatedOnUtc = DateTime.UtcNow, };
//add product via product service _productService.InsertProduct(prod);
//create category instance Category _category = new Category() { Name = "Laptops" };
Then you will have to create a data access project to access that database. I would create that in a new project separate from the noPcommerce projects. What is the purpose of this plugin? Are you exporting products from one place and importing into nopCommerce?