On 6/20/2019 at 12:48 PM, millien said:<?php require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.inc.php'); include(dirname(__FILE__) . '/init.php'); $res = Db::getInstance()->executeS('SELECT `id_product` FROM `'._DB_PREFIX_.'product` WHERE id_product = "76" '); echo "<p>(".date('Y/m/d H:i:s').") Starting to delete products...</p>"; if ($res) { foreach ($res as $row) { echo "<p>(".date('Y/m/d H:i:s').") Deleting product with ID <b>".$row['id_product']."</b>..."; $p = new Product($row['id_product']); if(!$p->delete()) { echo " <span style='color: red'>Error deleting this product!</span></p>"; } else { echo " <span style='color: green'>DELETED</span></p>"; } } } ?>
I tried one product product ID 76 after I run it the product deletes from backoffice , but in the DB I can se the row both in ps_product and ps_product_shop , We have multistore
I've just discovered that product->delete() has a bug that prevents it from deleting the product. Underneathdelete() checks what is the shop in your context and doesn't use the shop id used for loading the product. E.g.
$product = new Product(1234);
$product->delete();
It will delete the product in shop A, if you execute your script from shop A URL, only. It will not delete the product at all from other shops. To delete your product in shop B, you must execute your script from shop B URL.
If you try loading the product with shop B, it won't work, too.
$product = new Product(1234, false, null, shop_B_id);
$product->delete();
I've looked at PrestaShop Product->delete() and it seems that underneath it's taking the shop ID from your current context, not from the product when it was loaded.
To me that's a bug that should be fixed. If the product is loaded without id_shop, it should delete the product from all shops. If you load the product specifying id_shop, it should delete the product from the given id_shop, regardless of the domain it was executed from. I'm looking for solution.
UPDATE:
For product->delete() to work in multistore environment, you need to set shop's context it's executed in. E.g.
Shop::setContext(Shop::CONTEXT_ALL);
will delete the product from all shops.