How do you programmatically remove a specific product from a specific category using?
-
what you mean, if your product is in 2 category so one is deleted and other is remain ?– RohitCommented May 19, 2016 at 17:20
-
Yes, that's correct.– mockingbidCommented May 19, 2016 at 17:22
-
why do you not unset the category for product which you want to delete ?– RohitCommented May 19, 2016 at 17:23
-
Yeah by remove i mean unset, is there a method like setCategoryIds() ?– mockingbidCommented May 19, 2016 at 17:24
Add a comment
|
1 Answer
You should use Mage_Catalog_Model_Product::getCategoryIds()
to get existing categories, remove from that array your category in question and then use Mage_Catalog_Model_Product::setCategoryIds($updatedCategoryIds)
method to update the product/category associations, then save the product by calling its Mage_Catalog_Model_Product::save()
method.
-
is there a way without looking for all categories and updating the product with their ids expect for the one i don't want to be associated with ? Is there not any direct method ? Commented May 19, 2016 at 17:40
-
The method I have recommended you is the Magento way used by Magento codebase itself and that is the best way you can achieve your goal. Other methods either do the same thing that product model resource does, or imply working directly with database which is a no-no due to data corruption risk reasons but most importantly because you would need to write a lot more code than the recommended method has.– adjcoCommented May 19, 2016 at 17:49