Programatically add Products

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
i need to write a Plugin which Programatically add Products to nopCommerce. can anyone help me how can i achieve it ? please explain me with code.
11 年 前
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

Hope that helps!
11 年 前
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?
11 年 前
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.
11 年 前
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.
11 年 前
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,
            };

            productService.InsertProduct(prod);
        }
    }
}
11 年 前
Yes that looks like the right direction.

To map a product to a category you create a new ProductCategory and save it through the CategoryService, like this:


var productCategory = new ProductCategory
     {
        ProductId = myProduct.Id,
        Product = myProduct,
        Category = myCategory,
        CategoryId = myCategory.Id
     };

_categoryService.InsertProductCategory(productCategory);
11 年 前
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"
            };            

            //relate category & product
            var productCategory = new ProductCategory
            {
                ProductId = prod.Id,
                Product = prod,
                Category = _category,
                CategoryId = _category.Id
            };

            //add product category
            _categoryService.InsertProductCategory(productCategory);
        }
    }
}
11 年 前
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?
11 年 前
Can you guide me how can i do that ? i have aspstorefront database and i want to migrate its products and some related data to nopCommerce.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.