How to create a simple product using SOAP api in magento2
-
07-10-2020 - |
Pergunta
Here I have tried but I getting this error:
Fatal error: Uncaught SoapFault exception: [env:Sender] %fieldName is a required field.
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = '6ge34qaj7ugklona2in8fmmig93qmblx';
$wsdlUrl = 'http://localhost/magento214/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$productData = array(
'sku' => 'ZZ-TEST',
'name' => 'testproduct',
'visibility' => 4, /*'catalog',*/
'type_id' => 'simple',
'price' => 45,
'status' => 1,
'attribute_set_id' => 4,
'weight' => 1,
'custom_attributes' => array(
array( 'attribute_code' => 'category_ids', 'value' => ['3'] ),
array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ),
)
);
$productData = json_encode(array('product' => $productData));
// print_r($productData);exit;
$result = $soapClient->catalogProductRepositoryV1save($productData);
var_dump($result);
Solução
The code below works.
Tested on Magento 2.1.4
.
The error you received:
Fatal error: Uncaught SoapFault exception: [env:Sender] %fieldName is a required field.
was due to json_encode
ing $productData
. Magento simply could not find product
fieldName.
You don't need to json_encode
things and parameters should be typeId
and attributeSetId
.
require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');
$token = 'qdst8d40dfdfdfdfd43r3ererenx26f';
$wsdlUrl = 'http://m214.domain.com/soap/default?wsdl&services=catalogProductRepositoryV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$productData = array(
'sku' => 'ZZ-TEST',
'name' => 'testproduct',
'visibility' => 4,
'typeId' => 'simple',
'price' => 45,
'status' => 1,
'attributeSetId' => '4',
'weight' => 1,
);
$result = $soapClient->catalogProductRepositoryV1save(array('product' => $productData));
var_dump($result);
Disclaimer: I could not make description
, category_ids
and short_description
work though.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange