require_once('PSWebServiceLibrary.php');
$url = 'http://example.com';
$webService = new PrestaShopWebservice($url, 'EXAMPLEAPIKEY', false);
$opt['resource'] = 'products';
$opt['display'] = 'full';
$xml = $webService->get($opt);
$productNodes = $xml->products->children();
$products = array();
foreach ($productNodes as $product) {
$nameLanguage = $product->xpath('name/language[@id=1]');
$name = (string) $nameLanguage[0];
$idImage = (string) $product->id_default_image;
$image = '/img/p/';
for ($i = 0; $i < strlen($idImage); $i++) {
$image .= $idImage[$i] . '/';
}
$image .= $idImage . '.jpg';
$id = (int) $product->id;
$descriptionLanguage = $product->xpath('description/language[@id=1]');
$description = (string) $descriptionLanguage[0];
$path = '/index.php?controller=product&id_product=' . $product->id;
$products[] = array('name' => $name, 'image' => $image, 'id' => $id, 'description' => $description, 'path' => $path);
}
PrestaShop - retrieving products via API
-
25-06-2023 - |
Question
Is there a way to build up an array of PrestaShop products through the API so that I can use them in external application?
I simply want something like this:
array(
0 => array(
'name' => 'Some product',
'image' => 'path/to/image.jpg',
'id' => 1,
'description' => 'Some description of the product here',
'path' => 'path/to/product'
),
...
999 => array(
...
)
);
I know of the webservice call where array('resource' => 'products', 'display' => 'full')
but I really do not know how to get to the array I need from the content returned from this webservice call. All I want to do is to display a product scroller on an external website, where each image links to the product in the shop.
I am using Prestashop 1.5.6.2 and CodeIgniter 2.1.4. The product scroller must be shown in the CI app.
Edit 1
The closest I have come sofar is this: $product = new Product(1, false, 1);
for product number 1, but it does not contain the image.
Solution