I had to remove all the products assigned to a category that was used as trash bin for some reasons, the problem was that the amount of products was pretty massive (16000) and I had no chances to do it manually, so I made a small PHP script to do the job.
<?php
require_once "app/Mage.php";
Mage::app('admin');
$category_id = 146;
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $product){
$categories = $product->getCategoryIds();
if(in_array($category_id, $categories)) {
$product_load = Mage::getSingleton('catalog/product')->load($product->getId());
Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product_load));
$product_load->delete();
}
}
?>
Enjoy!
Pingback: How to Delete All Products Within a Category - Gomagento2
Buen día como puedo trabajarlo con multiples categorias?
I’m not sure I got it right, but I think you are asking how to do it for multiple categories.
You have several routes:
Pingback: How to Delete All Products Within a Category - MagentoHub
Pingback: How to Delete All Products Within a Category | DL-UAT
where do you put this code? in a file or you run mysql query?
Thank you
Hello Steven, in a file and then executed it via the command line as PHP will go in timeout if executed from the web.
Hi Soraph,
thank you for your quick reply.
Is there a specific location where I need to place this file?
Will it work on version 1.9.1?
Does this also work with Magento 1.4.1.1?
I did not had a chance to test it on versions before 1.5.x
Thanks for script.
I think you dont need load product, I replace this line :
$product_load = Mage::getSingleton(‘catalog/product’)->load($product->getId());
On a:
$product_load = $product;
And script works correctly, and dont make additional query
Nice one 🙂
Awesome code.You are life saver……………………….:D
I’m glad it helped you 🙂