I wrote a simple function to add a product to presta based on prestashop webservice library:
public function postPrestaProduct($id, $name, $description, $price, $ean, $category, $manufacturer) {
$webService = new \PrestaShopWebService('xxx', 'xxx', false);
$blankXml = $webService->get(['resource' => 'products', 'schema' => 'blank']);
$productFields = $blankXml->product->children();
$productFields->id = $id;
$productFields->name = '';
$productFields->name->addChild('language', $name);
$productFields->name->language->addAttribute('id', '2');
$productFields->description = '';
$productFields->description->addChild('language', $description);
$productFields->description->language->addAttribute('id', '2');
$productFields->price = $price;
$productFields->ean13 = $ean;
$productFields->id_category_default = isset($category) ? $category : null;
$productFields->associations->categories->category->id = isset($category) ? $category : null;
unset($productFields->position_in_category);
$productFields->id_manufacturer = $manufacturer;
$productFields->active = true;
$webService->add(['resource' => 'products', 'postXml' => $blankXml->asXML()]);
}
and every time this function is called the product is added to presta however every time it returns an error:
PrestaShopWebserviceException: HTTP response is empty in /path
which causes the loop to break.
Does anyone have any idea what could be causing this error here?
>Solution :
Walkaround:
The function seems to works for adding products but throws an exception due to an empty HTTP response. It may be related to the PrestaShop Webservice and how it handles the response.
If this is true, the solution is to catch the exception and investigate the cause of the problem. You can try modifying your function in this way:
public function postPrestaProduct($id, $name, $description, $price, $ean, $category, $manufacturer) {
try {
$webService = new \PrestaShopWebService('xxx', 'xxx', false);
// ... (rest of your code)
$webService->add(['resource' => 'products', 'postXml' => $blankXml->asXML()]);
} catch (\PrestaShopWebserviceException $e) {
if ($e->getMessage() == 'HTTP response is empty') {
// Log the error or handle it according to your needs
error_log('HTTP response is empty for product ID: ' . $id);
} else {
throw $e;
}
}
}
It will catch the PrestaShopWebserviceException exception and check if the message reads HTTP response is empty. If so, you can log the error and continue processing other products without breaking the loop. If it is another exception, you can throw it.