0

How do you programmatically remove a specific product from a specific category using?

4
  • what you mean, if your product is in 2 category so one is deleted and other is remain ?
    – Rohit
    Commented May 19, 2016 at 17:20
  • Yes, that's correct.
    – mockingbid
    Commented May 19, 2016 at 17:22
  • why do you not unset the category for product which you want to delete ?
    – Rohit
    Commented May 19, 2016 at 17:23
  • Yeah by remove i mean unset, is there a method like setCategoryIds() ?
    – mockingbid
    Commented May 19, 2016 at 17:24

1 Answer 1

1

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.

2
  • 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
    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.
    – adjco
    Commented May 19, 2016 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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