Return to Snippet

Revision: 39246
at January 15, 2011 02:42 by TheJasonParker


Initial Code
<?php

$storeid = 1;
$catid = 16;

deleteAllCategoryProducts($storeid,$catid);

function deleteAllCategoryProducts($storeid,$catid)
{
  require_once 'app/Mage.php';
  Mage::app( "default" )->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

  $_category = Mage::getModel('catalog/category')->setStoreId( $storeid );
  $_category = $_category->load($catid);

  $subcats = $_category->getAllChildren(true); // true to get as array
  
  foreach ($subcats as $subcatid) {
    echo 'Deleting from Category ID: ' . $subcatid . "\n";
    echo 'Deleting Product IDs: ';
    $pids = Mage::getResourceModel('catalog/product_collection')->setStoreId($storeid)->addCategoryFilter(Mage::getModel('catalog/category')->load($subcatid))->getAllIds();

    if(is_array($pids))
    {
      foreach ( $pids as $pid ) {
        echo $pid . ' ';
        $product = Mage::getSingleton('catalog/product')->load($pid)->delete();
        unset ($pids[$pid]);
      }
    }
    echo "\n";

  }
} 
?>

Initial URL


Initial Description
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.

Initial Title
Delete All Category Products

Initial Tags
magento

Initial Language
PHP