cancel
Showing results for 
Search instead for 
Did you mean: 

About Magento 2 Create New Product by API

About Magento 2 Create New Product by API

Hi, All

I am developing a program, using Magento V2 SOAP API, but I can not find how to Create Product, the API only provide me DeleteEntity, GetList Entity, and Save Entity. I have tried to use catalogProductRepositoryV1Save, but got an exception "Can not save product". But I can fetch t all product details using GetList.

Any one can help me? Thanks,  my code is below:

using (OperationContextScope scope = new OperationContextScope(mservice.InnerChannel))
                {
                    var httpRequestProperty = new HttpRequestMessageProperty();

                    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + accessToken;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;                  

                    CatalogProductRepositoryV1SaveRequest x = new CatalogProductRepositoryV1SaveRequest();

                    var t = new CatalogDataProductInterface();
                    t.attributeSetId = 4;
                    t.name = "Jason Test";
                    t.price = 456;
                    //t.priceSpecified = false;
                    t.sku = "34567890";
                    t.visibility = 1;
                    t.weight = 3;
                    t.typeId = "Simple";
                    t.status = 1;
                    t.createdAt = t.updatedAt = DateTime.Now.ToShortDateString();
                    t.productLinks = null;
                    t.attributeSetIdSpecified = true;
                    t.extensionAttributes = null;
                    t.mediaGalleryEntries = null;
                    t.id = 0;

                    var m = new FrameworkAttributeInterface[3];
                    //var w = new CatalogDataProductExtensionInterface();


                    // 1st Attribute
                    m[0] = new FrameworkAttributeInterface { attributeCode = "C1", value = "I3" };
                    // 2nd attribute
                    m[1] = new FrameworkAttributeInterface { attributeCode = "O2", value = "W8" };
                    m[2] = new FrameworkAttributeInterface { attributeCode = "S3", value = "15" };

                    t.customAttributes = m;
                    x.product = t;
                    var DEST = mservice.catalogProductRepositoryV1Save(x);
3 REPLIES 3

Re: About Magento 2 Create New Product by API

How to create or update a product programmatically with the /V1/products Web API? https://mage2.pro/t/1296

Re: About Magento 2 Create New Product by API

I have the same requirement adding the product using default Magento2 REST API .
Step1. Generate admin token:
I am using token for authorization, so create an admin token using this URL Http://{baseurl}/rest/V1/integration/admin/token

Step2. Add product :
For adding the product, I am using following URL http://magentogit.com/rest/V1/products/{SKU} , this is magento2 default API using put method.
For example:
   

http://baseurl/rest/V1/products/B201-SKU
header:
Content-Type - application/json
Authorization - Bearer token
Body:
{
"product": {
"sku": "B201-SKU",
"name": "B202",
"price": 30.00,
"status": 1,
"type_id": "simple",
"attribute_set_id":4,
"weight": 1
}
}



You can add more data for the product after this call chek your product from admin.
You are done.

Re: About Magento 2 Create New Product by API

Hi,

 

I got error like this.

 

main.CRITICAL: LogicException: Property "0" does not have corresponding setter in class