2

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 get all product details using GetList APP.

anyone can help me? code as 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);
1

My working example for create product in M2 by SOAP You should djust create token in BO and add it in header like Authorization: Bearer

$wsdl = "http://magento2-ce.local/soap/default?services=catalogProductRepositoryV1";
$xml_post_string = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:def="http://magento2-ce.local/soap/default?services=catalogProductRepositoryV1">
    <soap:Header/>
    <soap:Body>
        <def:catalogProductRepositoryV1SaveRequest>
            <product>
                <sku>1005001</sku>
                <name>TEST PRODUCT</name>
                <attributeSetId>4</attributeSetId>
                <price>19.99</price>
                <status>1</status>
                <visibility>4</visibility>
                <typeId>simple</typeId>
                <weight>1</weight>
            </product>
        </def:catalogProductRepositoryV1SaveRequest>
    </soap:Body>
</soap:Envelope>';

$headers = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "Authorization: Bearer wcawo83o8098wr67hj0msjgkxsj1qpuq",
    "Content-length: " . strlen($xml_post_string),
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $wsdl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);

$fp = fopen("soap.xml", "w");
fwrite($fp, $result);
fclose($fp);

var_dump($result);
  • thanks goral, I have create the correct token, and I can fetch product's detail, but I still got an exception while I want to Create a New Product. – Jason Xia Apr 18 '16 at 3:41
  • Please provide result of var_dump($result); – goral Apr 18 '16 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.