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 ? – Rohit May 19 '16 at 17:20
  • Yes, that's correct. – mockingbid May 19 '16 at 17:22
  • why do you not unset the category for product which you want to delete ? – Rohit May 19 '16 at 17:23
  • Yeah by remove i mean unset, is there a method like setCategoryIds() ? – mockingbid May 19 '16 at 17:24

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 ? – mockingbid May 19 '16 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. – adjco May 19 '16 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.