Hello,
I have succesfully uploaded about 5000 products which i got from an API call…
I have used a runner because otherwise i get an error 500 because of the overload…
I used this as test script in my Getproducts call…
var jsonData = pm.response.json();
pm.environment.set("productData", JSON.stringify(jsonData));
In my Post call to add the products in batches to woocommerce i used this pre-request-script:
var productData = pm.environment.get("productData");
if (productData) {
var products = JSON.parse(productData);
var batchSize = 100; // Batchgrootte instellen
var startIndex = pm.environment.get("startIndex") || 0; // Haal de startindex op uit de omgevingsvariabele of gebruik 0
var batchProducts = products.slice(startIndex, startIndex + batchSize);
var createdProducts = [];
for (var i = 0; i < batchProducts.length; i++) {
var product = batchProducts[i];
if (product.statusgroup === "New") {
var postBody = {
"name": product.title,
"description": product.longdescription,
"short_description": product.description,
"sku": product.itemcode,
"regular_price": product.suggestprice.toString(),
"manage_stock": true,
"stock_quantity": product.stockfree + product.externalstock,
"backorders": (product.backorder === 1) ? "notify" : "no",
"categories": [
{
"name": product.categoryname2
}
],
"images": [
{
"src": product.productimage
}
]
};
createdProducts.push(postBody);
}
}
if (createdProducts.length > 0) {
// Pas de aanroep aan voor batchverwerking
pm.request.url = 'https://xxxxxxxxx/wp-json/wc/v3/products/batch';
pm.request.method = 'POST';
pm.request.header = {
'Content-Type': 'application/json',
'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
};
pm.request.body.raw = JSON.stringify({ "create": createdProducts });
// Update de startindex voor de volgende batch
var nextIndex = startIndex + batchSize;
pm.environment.set("startIndex", nextIndex);
} else {
console.log("Geen nieuwe producten om toe te voegen.");
}
} else {
console.error("productData is niet beschikbaar in de omgevingsvariabele.");
}
Apart from the categories, the sku, prices and stock all is being added nicely… If someone could tell me how to fix the categories i would be gratefull… I tried to use toString() and parseInt() but it made no difference…
The next problem i am facing is I am using a seperate call “Getproductimages” which gives me back a JSON like this:
[
{
"mediaparentid": 8509838,
"itemcode": "0318C001",
"versions": [
{
"mediaid": 8510448,
"url": "https://productimages.nl/media/image/high/9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae.jpg",
"quality": "high",
"filetype": "jpg",
"filesize": 62347,
"width": 1000,
"height": 1000,
"checksum": "9bfc9ab88f9e32357af0ab85b21bc0c71c19dec9d782b60c3d4c5701222108ae"
},
{
"mediaid": 8510446,
"url": "https://productimages.nl/media/image/low/a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060.jpg",
"quality": "low",
"filetype": "jpg",
"filesize": 4606,
"width": 200,
"height": 200,
"checksum": "a9318850c82328085aa3cad96b7e5c6f4acfc4fcf7c9a704269130838cddb060"
},
{
"mediaid": 8510447,
"url": "https://productimages.nl/media/image/medium/e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846.jpg",
"quality": "medium",
"filetype": "jpg",
"filesize": 18764,
"width": 500,
"height": 500,
"checksum": "e8ef719f4a232a5463fce0d9b61ad395ec9ac57a7a995d49739e1ccb015e5846"
},
{
"mediaid": 8510445,
"url": "https://productimages.nl/media/image/thumb/b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895.jpg",
"quality": "thumb",
"filetype": "jpg",
"filesize": 1304,
"width": 75,
"height": 75,
"checksum": "b8aeee6abb32bafe38979d7901897025ce5b5366711c062daeea912a4e023895"
}
]
},
I want to add the pictures which has the quality: “high” to be added to the products… The products are allready created, the itemcode from the productimages could be used to correspond with the SKU in woocommerce… Would it be better to do this before i create the products and add the images upon creating the products? And can’t this to work and some help would be very awesome!