Delete All Category Products


/ Published in: PHP
Save to your folder(s)

Change the category id and store id at the top of the script before running it. You can also remove the echo statements if you don’t want any output.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $storeid = 1;
  4. $catid = 16;
  5.  
  6. deleteAllCategoryProducts($storeid,$catid);
  7.  
  8. function deleteAllCategoryProducts($storeid,$catid)
  9. {
  10. require_once 'app/Mage.php';
  11. Mage::app( "default" )->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  12.  
  13. $_category = Mage::getModel('catalog/category')->setStoreId( $storeid );
  14. $_category = $_category->load($catid);
  15.  
  16. $subcats = $_category->getAllChildren(true); // true to get as array
  17.  
  18. foreach ($subcats as $subcatid) {
  19. echo 'Deleting from Category ID: ' . $subcatid . "\n";
  20. echo 'Deleting Product IDs: ';
  21. $pids = Mage::getResourceModel('catalog/product_collection')->setStoreId($storeid)->addCategoryFilter(Mage::getModel('catalog/category')->load($subcatid))->getAllIds();
  22.  
  23. if(is_array($pids))
  24. {
  25. foreach ( $pids as $pid ) {
  26. echo $pid . ' ';
  27. $product = Mage::getSingleton('catalog/product')->load($pid)->delete();
  28. unset ($pids[$pid]);
  29. }
  30. }
  31. echo "\n";
  32.  
  33. }
  34. }
  35. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.