Jump to content

Delete all products and combinations using php


davurpi

Recommended Posts

I am updating my catalog automatically forcing IDs, and every time I run the php Importer (a script I found in this forum: https://www.prestashop.com/forums/topic/441824-script-importar-productos/), before, I always delete manually products and combinations from the backoffice, as you may have the new catalog and other new products are gone (do not use the Prestashop Cleaner also erase categories, but do not want to delete).

I decided to add functions to delete, at the beginning of the importer and I tested with:


$productos = Product::getProducts();
foreach ($productos as $index => $producto) {
$producto->deleteProduct();
}

$atributos = Attribute::getAttributes();
foreach ($atributos as $index => $atributo) {
$atributo->deleteAttribute();
}

 
But I put something wrong (I do not have much idea) and not working.
Can you tell me what I do wrong?
On the other hand, maybe it's not a good idea to remove, also let me know if there is another alternative.

Thank you!
Best regards,
David Urpí

Link to comment
Share on other sites

You shouldn't need to delete the attributes separately. Also if you're using Advanced Stock Management then there are certain conditions where the product delete will fail (stock >0 or resupply order pending I think). The product may also not be completely deleted if you have MultiShop enabled.

 

Assuming the above is ok I would just call:

$results = Product::getSimpleProducts();
foreach ($results as $result) {
    $product = new Product($result['id_product']);
    $product->delete();
}

That should take care of everything (including the attributes).

 

EDIT: I should say that this will delete EVERYTHING to do with the product including any images, discounts, specific pricing etc.

Edited by Paul C (see edit history)
Link to comment
Share on other sites

Thank you, very much.

I don't using Advanced Stock Management and i have Multishop disabled. But when i execute this code, i obtain:

 

Warning: Missing argument 1 for ProductCore::getSimpleProducts(), called in /.../script_import.php on line 39 and defined in /.../classes/Product.php on line 1161 Notice: Trying to get property of non-object in /.../classes/Product.php on line 1167 Notice: Undefined variable: id_lang in /.../classes/Product.php on line 1174

 

And not delete products.

Line 39 was the first line of your code: $results = Product::getSimpleProducts();

I have to add id_lang in any place on code?

 

Thank you!

David

Edited by davurpi (see edit history)
Link to comment
Share on other sites

Sorry, yes my bad. Shouldn't do these things from memory! I thought it defaulted. You'll need to get the context in the script if you don't already e.g. with :

$context = Context::getContext();

So you can pass the language to the call:

$results = Product::getSimpleProducts((int)$context->language->id);
Edited by Paul C (see edit history)
Link to comment
Share on other sites

It really depends on your import script whether you need to delete them or not. The actual relationships to the products are gone and what's left should get reused when you assign attributes/groups to the new products added - but it depends on how the script is written.

 

In general you should always try and use the delete() member function of classes so if you really need to remove them all you could try:

$results = AttributeGroup::getAttributesGroups((int)$context->language->id);
foreach ($results as $result) {
  $group = new AttributeGroup($result['id_attribute_group']);
  $group->delete();
}

I haven't tried this but it should remove any unused attributes so doing this after the first one to delete the products should clean them out.

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...