Mar 22, 2012

Magento: Deleting Product

A simple way to delete product is:
$productId = 1;
Mage::getSingleton('catalog/product')
  ->load($productId)
  ->delete();
If you want to delete product by category
$collection = Mage::getSingleton('catalog/category')
  ->load(YOUR_CATEGORY_ID)
  ->getproductCollection();

// If you want to filter by attribute
$collection->addAttributeToFilter('YOUR_ATTRIBUTE_CODE', 'ATTRIBUTE_VALUE')
foreach($collection as $product){
   $product =Mage::getSingleton('catalog/product')
     ->load($product->getId())
   try {
   // Im not sure what is the use of this part
   Mage::dispatchEvent(
     'catalog_controller_product_delete',
     array('product' => $product));
   
   // Finally we not able to delete the product
   $product->delete();
   }catch (Exception $e) {
     echo "Can't delete product w/ id: $product";
   }
}
Reference: Delete magento Product

No comments: