nopCommerce API

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 Jahre weitere
I couldnt find any api documentation online so i had a look at what classes are in the dlls.

I found using NopSolutions.NopCommerce.BusinessLogic.Products allows me to create a product like so:

Product p = new Product();
                p.Name = "test product";
                p.ShortDescription = "test description";
                p.FullDescription = "full test description";
                p.TemplateId = 4;
                p.ShowOnHomePage = false;
                p.AllowCustomerRatings = false;
                p.AllowCustomerReviews = false;
                p.Published = true;
                p.CreatedOn = DateTime.Today;
when i run this i get no errors but it doesnt look like the product gets created. Is there a save method or anything im missing???
13 Jahre weitere
philis wrote:
I couldnt find any api documentation online so i had a look at what classes are in the dlls.

I found using NopSolutions.NopCommerce.BusinessLogic.Products allows me to create a product like so:

Product p = new Product();
                p.Name = "test product";
                p.ShortDescription = "test description";
                p.FullDescription = "full test description";
                p.TemplateId = 4;
                p.ShowOnHomePage = false;
                p.AllowCustomerRatings = false;
                p.AllowCustomerReviews = false;
                p.Published = true;
                p.CreatedOn = DateTime.Today;
when i run this i get no errors but it doesnt look like the product gets created. Is there a save method or anything im missing???



If you look under the folder "Administration\Modules\ProductAdd.ascx.cs"

The code refered to create a product is
            var product = new Product()
            {
                Name = name,
                ShortDescription = shortDescription,
                FullDescription = fullDescription,
                AdminComment = adminComment,
                TemplateId = templateId,
                ShowOnHomePage = showOnHomePage,
                AllowCustomerReviews = allowCustomerReviews,
                AllowCustomerRatings = allowCustomerRatings,
                Published = published,
                CreatedOn = nowDT,
                UpdatedOn = nowDT
            };

            this.ProductService.InsertProduct(product);

Although you seem to be creating the 'variable' product, I think the stuff in bold is what actually commits it to the website.
Also, don't forget about the product variant!
13 Jahre weitere
thanks for the reply. i think im on a different version though because my productadd.ascx.cs doesnt have that method. im on v 1.90. what does the 'this' refer to in your example? should there be a using method wrapped around it?
13 Jahre weitere
i found the insertproduct method in the product services class. i created a productservices object, which required a 'NopObjectContext' to construct it...

var dbContext = IoC.Resolve<NopObjectContext>();

                ProductService ps = new ProductService();
                ps.InsertProduct(product);

so i tried creating a NopObjectContext object and i got:
              
               var dbContext = IoC.Resolve<NopObjectContext>();

                ProductService ps = new ProductService(dbContext );
                ps.InsertProduct(product);

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0012: The type 'System.Data.Objects.ObjectContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Source Error:


Line 176:                };
Line 177:
Line 178:                var dbContext = IoC.Resolve<NopObjectContext>();
Line 179:
Line 180:                ProductService ps = new ProductService(dbContext);


:(

i dont understand, visual studio seems to recognise it.
13 Jahre weitere
philis wrote:
thanks for the reply. i think im on a different version though because my productadd.ascx.cs doesnt have that method. im on v 1.90. what does the 'this' refer to in your example? should there be a using method wrapped around it?


I'm on 1.90 also, strange that you don't see the code in your source.  That method i think is referenced elsewhere, but the function is called in the productadd.ascx.cs

I tried finding the actual code behind that function and couldn't (to see how it's actually adding the information to the database).

the 'this' is what was in the code already, i just copy/pasted from the file to help give you a reference to how nopcommerce was doing what you are trying to do.
13 Jahre weitere
weird! can you paste your full cs code so i can take a look at it?

thanks for your help!
13 Jahre weitere
philis wrote:
weird! can you paste your full cs code so i can take a look at it?

thanks for your help!


opps, my bad, it's ProductInfoAdd.ascx.cs that has the code

Let me know if you don't see the code in there and i'll pm it to you.
13 Jahre weitere
fantastic, thats everything i needed!
13 Jahre weitere
I'm trying to do pretty much the same thing but from an external library.


       public void InsertProducts()
        {
            Product OP = new Product();
            OP.Name = "Bla";
            OP.ShortDescription = "efwfewf";
            DependencyResolverFactory oDRF = new DependencyResolverFactory();
            IoC.InitializeWith(oDRF);
            var dbContext = IoC.Resolve<NopObjectContext>("NopObjectContext");
            ProductService oPSvc = new ProductService(dbContext);
            oPSvc.InsertProduct(OP);
        }

I keep crashing when I try to Resolve.
with the following Inner Exception:
"The type NopObjectContext has multiple constructors of length 1. Unable to disambiguate."


Is there an easier wat to pass the ObjectContext to the product service?
13 Jahre weitere
I have modified my code after reading a few posts to the following: but I'm getting the error mentioned bellow.


        public void InsertProducts()
        {
            Product OP = new Product();
            OP.Name = "Bla";
            OP.ShortDescription = "efwfewf";

            NopConfig.Init();
            IoC.InitializeWith(new DependencyResolverFactory());
            var oPSvc = IoC.Resolve<IProductService>();
            oPSvc.InsertProduct(OP);
        }


Resolution of the dependency failed, type = "NopSolutions.NopCommerce.BusinessLogic.Products.IProductService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type NopObjectContext has multiple constructors of length 1. Unable to disambiguate.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving NopSolutions.NopCommerce.BusinessLogic.Products.ProductService,(none) (mapped from NopSolutions.NopCommerce.BusinessLogic.Products.IProductService, (none))
  Resolving parameter "context" of constructor NopSolutions.NopCommerce.BusinessLogic.Products.ProductService(NopSolutions.NopCommerce.BusinessLogic.Data.NopObjectContext context)
    Resolving NopSolutions.NopCommerce.BusinessLogic.Data.NopObjectContext,(none)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.