You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
please provide a (or document an existing) way to create / import data using JSON files.
example where it came up: a 3rd party tool generates product drafts as JSON which should be used in ProductCreateRequests instead of building the Product Drafts on the PHP API.
what happened is that the user did not use the PHP SDK for that but did it all manually (and ran into OAuth issues). It's propbably just documentation, but I think it would be helpful to provide explicit named constructors "ofDraftJson" or similar to either the Create Requests or the Draft Model Objects.
It's performance sensitive (batch imports), so there should not be a full object mapping roundtrip involved (validation is done platform-side anyways). JSON could be provided either as a String or as a PHP associative array.
The text was updated successfully, but these errors were encountered:
Hi, as there seems to be demand: Every Model Object in the SDK (in this case you're probably looking for the *Draft ones) can be created via its fromArray() function that takes the associative array created by PHP's own json_decode().
The same applies in the opposite direction: all the model objects are done to be cleanly serialized to JSON via json_encode($myCommercetoolsModel) .
So practically spoken the following (UNTESTED!) code is the pattern how you can do a bulk import using PHP:
<?phpuseCommercetools\Core\Model\Product\ProductDraft;
useCommercetools\Core\Request\Products\ProductCreateRequest;
useCommercetools\Core\Client;
useCommercetools\Core\Config;
$client = Client::ofConfig(Config::fromArray(["clientId" => ""/* to do complete config*/]));
// for every batch of e.g. 100 products do:// for each product:$productDraftAsArray = json_decode(file_get_contents("./myFile.json"));
$productDraft = ProductDraft::fromArray($productDraft);
$addProductRequest = ProductCreateRequest::ofDraft($productDraft);
$client->addBatchRequest($addProductRequest);
//$client->executeBatch(); // SDK will do the requests with a parallelity of (sensible default) 25echo("added a batch of products");
//// do next batch
That being said, IF you're importing Products, Prices (JSON or CSV) or other stuff (just CSV) you will still be faster and more robust using the Command-Line import / export tooling. Especially if you're updating / synchronizing and not just initially bulk importing. Generating the necessary update actions by "diffing" the current and new JSON is a real pain these tools do for you. https://github.com/sphereio/sphere-product-import // http://sphereio.github.io/nodejs/
(@jayS-de FYI in case you have something better or tested available)
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
please provide a (or document an existing) way to create / import data using JSON files.
example where it came up: a 3rd party tool generates product drafts as JSON which should be used in ProductCreateRequests instead of building the Product Drafts on the PHP API.
what happened is that the user did not use the PHP SDK for that but did it all manually (and ran into OAuth issues). It's propbably just documentation, but I think it would be helpful to provide explicit named constructors "ofDraftJson" or similar to either the Create Requests or the Draft Model Objects.
It's performance sensitive (batch imports), so there should not be a full object mapping roundtrip involved (validation is done platform-side anyways). JSON could be provided either as a String or as a PHP associative array.
The text was updated successfully, but these errors were encountered: