Magento empty a category (1.5)
I'm wondering if there is a quick way to remove al开发者_开发技巧l products from a particular category? Or am I stuck with getting all the categories products then looping through and removing the category from each one?
I'm planning on running this on a cron.
Edit: Currently I'm using setCategoryIds() but it seems a very inefficient way of doing this.
Thanks!
The way to do it is indeed to loop through each product;
set an array of category ids, $ids then use the setCategoryIds($ids) method in the Mage_Catalog_Model_Product to set the category id for the product.
Let me know if you want the actual code for this or if you are looking for a different way.
Good luck.
I don't know about cron, but you could use the import/export functionnality. Or, depending on how much products are in those categories, you could use the bulk procesing function in the admin's products grid. I have a module to filter this grid by categories: if you want it just let me know and I'll upload it somewhere. oops, the category is not available by bulk attributes updating. but the import/export will do just fine.
This isn't tested, but something like this should do it:
$category = Mage::getModel("catalog/category")->load($id);
$products = $category->getProductCollection();
foreach($products as $product) {
$productCategories = $product->getCategoryIds();
$index = array_search($productCategories, $id);
unset($productCategories[$index]);
$product->setCategoryIds($productCategories);
$product->save();
}
精彩评论