Jump to content

Delete multiple unused products from catalogue


Recommended Posts

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 by [email protected] (see edit history)
Link to comment
Share on other sites

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 by Eolia (see edit history)
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...