[email protected] Posted July 22, 2014 Share Posted July 22, 2014 (edited) Hi, I´d like to delete all not used (= not used in any order) obsolete products from catalogue and therefore I´ve prepared the following script: <?php include (dirname(__FILE__).'/config/config.inc.php'); include (dirname(__FILE__).'/init.php'); $defaultLanguageId = intval(Configuration::get('PS_LANG_DEFAULT')); $home = Configuration::get('PS_HOME_CATEGORY'); $table_product = _DB_PREFIX_.'product'; $table_orders = _DB_PREFIX_.'order_detail'; include_once('config/settings.inc.php'); //připojení k databázi @$spojenie = MySQL_Connect(_DB_SERVER_ ,_DB_USER_, _DB_PASSWD_); @MySQL_Select_DB(_DB_NAME_); if (!$spojenie): echo "Spatne pristupove udaje do databaze!!!"; endif; mysql_query("SET NAMES 'utf8'"); $result = mysql_query("SELECT a.id_product FROM $table_product AS a LEFT JOIN $table_orders AS b ON a.id_product = b.product_id WHERE a.active =0 AND b.product_id IS NULL") or die("Chyba ve funkci clear_cat1 : " . mysql_error()); while ( $row = mysql_fetch_assoc($result) ) { echo "Deleting product ID: ".$row[id_product]."<br>"; $product = new Product((int)$row[id_product]); $product->delete(); } mysql_free_result($result); ?> The problem is that although there are a lot of such products in catalogue, but the script delete only the first found product and than breaks. Is there any internal restriction in Prestashop that multiple deletion of products is restricted? Or am I missing some parameter to set? PS: If I correct the script only to print the selected product ID´s, it works. Means the break comes after $product->delete(); . Edited July 22, 2014 by [email protected] (see edit history) Link to comment Share on other sites More sharing options...
Eolia Posted July 23, 2014 Share Posted July 23, 2014 (edited) Hi, I´d like to delete all not used (= not used in any order) obsolete products from catalogue and therefore I´ve prepared the following script: <?php include (dirname(__FILE__).'/config/config.inc.php'); include (dirname(__FILE__).'/init.php'); $defaultLanguageId = intval(Configuration::get('PS_LANG_DEFAULT')); $home = Configuration::get('PS_HOME_CATEGORY'); $table_product = _DB_PREFIX_.'product'; $table_orders = _DB_PREFIX_.'order_detail'; include_once('config/settings.inc.php'); //připojení k databázi @$spojenie = MySQL_Connect(_DB_SERVER_ ,_DB_USER_, _DB_PASSWD_); @MySQL_Select_DB(_DB_NAME_); if (!$spojenie): echo "Spatne pristupove udaje do databaze!!!"; endif; mysql_query("SET NAMES 'utf8'"); $result = mysql_query("SELECT a.id_product FROM $table_product AS a LEFT JOIN $table_orders AS b ON a.id_product = b.product_id WHERE a.active =0 AND b.product_id IS NULL") or die("Chyba ve funkci clear_cat1 : " . mysql_error()); while ( $row = mysql_fetch_assoc($result) ) { echo "Deleting product ID: ".$row[id_product]."<br>"; $product = new Product((int)$row[id_product]); $product->delete(); } mysql_free_result($result); ?> The problem is that although there are a lot of such products in catalogue, but the script delete only the first found product and than breaks. Is there any internal restriction in Prestashop that multiple deletion of products is restricted? Or am I missing some parameter to set? PS: If I correct the script only to print the selected product ID´s, it works. Means the break comes after $product->delete(); . Hi, Try this: <?php // Debug Mode for this script error_reporting(E_ALL); ini_set("display_startup_errors", "1"); ini_set("display_errors", "1"); // Required files include (dirname(__FILE__).'/config/config.inc.php'); include (dirname(__FILE__).'/init.php'); include_once('config/settings.inc.php'); //select inactive products from database $s = 'SELECT a.id_product FROM '._DB_PREFIX_.'product AS a LEFT JOIN '._DB_PREFIX_.'order_detail AS b ON a.id_product = b.product_id WHERE a.active =0 AND b.product_id IS NULL'; $tab = Db::getInstance()->executeS($s); /* delete the product but : * * @since 1.5.0 * It is NOT possible to delete a product if there are currently: * - physical stock for this product * - supply order(s) for this product */ $count = 0; if(!empty($tab)) { foreach($tab as $products_tab) { foreach($products_tab as $id_product) { $product = new Product((int)$id_product); if($product->delete()) { echo 'Product ('.$id_product.') deleted'; $count++; } else echo 'Product ('.$id_product.') not deleted'; } } echo '<br />'.$count . ' Product(s) deleted'; } else echo 'None product matches to be deleted.'; ?> Edited July 23, 2014 by Eolia (see edit history) Link to comment Share on other sites More sharing options...
[email protected] Posted July 23, 2014 Author Share Posted July 23, 2014 Hi, thank you but the result is the same - only 1 product deleted. And no error message and nothing printed on the screen. Means the script is again interrupted after ... if($product->delete()). Link to comment Share on other sites More sharing options...
Eolia Posted July 23, 2014 Share Posted July 23, 2014 Oh it's strange, it works for me (5) products deleted Link to comment Share on other sites More sharing options...
[email protected] Posted July 23, 2014 Author Share Posted July 23, 2014 This is why I think I have something restricted in settings But I don´t know where it could be ... Link to comment Share on other sites More sharing options...
[email protected] Posted July 23, 2014 Author Share Posted July 23, 2014 I made a tests and the result is: the script is working with Presta 1.6 but not working with old Presta 1.3 I´m using Thank you for your help, I´m just working on upgrade to 1.6 version. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now