Jump to content

can't delete product with $product->delete()


Recommended Posts

Hi all,

 

Hopefully someone can point out what I'm doing wrong?

 

I'm trying to delete every product from an array. But this doesn't seem to work.

 

When I use an SQL Delete statement it works correctly:

$proddeleted = Db::getInstance()->Execute('
DELETE FROM `'.pSQL(_DB_PREFIX_).'product`
WHERE reference = "'.($prodref).'"
');

But this only deletes the product record, not the prod_lang etc...

 

So I'm trying to user $product->delete()

$prodref = 'CUST'.$quoteid;
$products = Db::getInstance()->ExecuteS('
SELECT * FROM `'.pSQL(_DB_PREFIX_).'product`
WHERE reference = "'.($prodref).'"
');

foreach ($products as $id_product) {
  $product = new Product((int)$id_product);
  $product->delete();
}

Thanks all!!

 

  • Like 1
Link to comment
Share on other sites

Seems you're getting all columns from the table and you've forgotten that the result will have the column names as the indexes of the array. Try the following instead:

$prodref = 'CUST'.$quoteid;
$products = Db::getInstance()->ExecuteS('
SELECT `id_product` FROM `'.pSQL(_DB_PREFIX_).'product`
WHERE reference = "'.($prodref).'"
');

if (is_array($products) && count($products) > 0) {
  foreach ($products as $product_array) {
    $product = new Product((int)$product_array['id_product']);
    $product->delete();
  }
}

I haven't tested this code, so you'll have to test it yourself.

  • Like 1
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...