In Manage Products, before saving the product, I want to check whether the product already exists or not under the category. For example, I would like to check whether the Product ID 1 already exists or not under category id 1.

Thank you.

  • Can you not just look at the assigned categories? Sep 25 '14 at 11:04

You can get the category ids from the db like this:
Let's say $_product is the current product.

$categoryIds = Mage::getResourceModel('catalog/product')->getCategoryIds($_product);

If you do this before saving the product you will get the category ids that are currently stored in the db even if you changed them on the product instance.
I assume you want to perform some actions when the product is saved.
For this create an observer on the event catalog_product_save_before that looks like this:

public function productSaveBefore($observer) {
     $product = $observer->getEvent()->getProduct();
     $oldCategoryIds = Mage::getResourceModel('catalog/product')->getCategoryIds($product);
     //do your magic here with $oldCategoryIds
  • Hi, Marius, thank you so much! I will try your code and I will get back to you. Thanks a lot.
    – May18
    Sep 25 '14 at 15:08
  • Hi, Marius, it works. Thank you so much! Now, I am going to accept your answer. Once again, thanks!
    – May18
    Sep 28 '14 at 15:22

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.