Jump to content

Delete Product Images Programmatically [SOLVED]


ayfasoft

Recommended Posts

Hi. I am creating a module for Prestashop for adding and updating product images externally through a .csv file.

 

The actual module adds successfully the product image (if I change the "cover" attribute to false, It will add normal images), with this code:

$id_product = $product->id;
$url = 'image.jpg';
$shops = Shop::getShops(true, null, true);    
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add())
{
	$image->associateTo($shops);
	if (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', true))
	{
	
		$image->delete();
	}
}

Now that I want to do is, if I need to update a product, delete all the images (cover included) and re-update the new ones I will pass to the script.

 

SOLUTION: the only thing you need to do after creating or updating the product (add() or save()) is to call "$product->deleteImages();". This line will restart the images of the product to 0 and it will allow you to add new ones.

Edited by ayfasoft (see edit history)
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...