Home > Back-end >  Add multiple images to product in prestashop
Add multiple images to product in prestashop

Time:06-17

I would like to add several images in my product, the problem is that I don't know how to do it, because for the moment I can only add one image via url! If someone has the solution or documentation, I would be interested!

Thanks !

My code :

$xml = simplexml_load_file('php://input');
$product = new Product($xml->id);
$product->category = [2, 7];
$product->id_category_default = (int)$xml->id_category_default; //categories
$product->name = $xml->name; //nom produit
$product->price = $xml->price; //prix
$product->unit_price = $xml->unit_price; //prix unitaire
$product->wholesale_price = $xml->wholesale_price; //prix de gros
$product->tax_rate = $xml->tax_rate;
$product->ecotax = $xml->ecotax;
$product->ean13 = $xml->ean13; //code barre
$product->description = $xml->description; //description
$product->description_short = $xml->description_short; //petite description
$product->reference = $xml->reference; //reference
$product->weight = $xml->weight; //poids
$product->height = $xml->height; //hauteur
$product->width = $xml->width; //largeur
$product->depth = $xml->depth; //profondeur
$product->minimal_quantity = (int)$xml->minimal_quantity; //
$product->available_date = $xml->available_date; //
$product->delivery_in_stock = $xml->delivery_in_stock;
$product->delivery_out_stock = $xml->delivery_out_stock;
$product->additional_shipping_cost = $xml->shipping_cost;
$product->indexed = 1;
$product->active = true;
if ($xml->online_only == "true"){
    $product->online_only = true;
}else{
    $product->online_only = false;
}
if ($xml->available_for_order == "true"){
    $product->available_for_order = true;
}else{
    $product->available_for_order = false;
}
if ($xml->show_price == "true"){
    $product->show_price = true;
}else{
    $product->show_price = false;
}
$product->save();
$product->updateCategories($product->category);
$e = $product->getDefaultIdProductAttribute();
StockAvailable::setQuantity($product->id, $e, $xml->quantity);
$image = new Image();
$image->id_product = $product->id;
$image->position = Image::getHighestPosition($product->id)   1;
$image->cover = true;
$image->save();
AdminImportControllerCore::copyImg((int)$product->id, (int)$image->id, $xml->urlImage, 'products', false);

CodePudding user response:

$nb = count($xml->urlImage) - 1;
$i = 0;
$a = 1;
while ($i <= $nb) {
    $image = new Image();
    $image->id_product = $product->id;
    $image->position = Image::getHighestPosition($product->id)   $a  ;
    if ($i >= 1){
        $image->cover = false;
    }else{
        $image->cover = true;
    }
    $image->image_format = 'jpg';
    $image->save();
    $this->copyImg((int)$product->id, (int)$image->id, $xml->urlImage[$i  ], 'products', false);
}
  • Related