In my website I have two categories let us assume Boys toys and Girls toys.

So i want to merge them into a single category named Toys.

Boys toys Category has 183 products.

Girls toys category has 283 products.

Now how do I find 100 products programatically which are in both categories.

Is there any way to find out or manually only do it or it will automatically Marge

  • The question is not clear. You have two categories, Boys Toys (183 products), Girls Toys (283 Products). After that what exactly you want is not clear. Please clarify your requirement. Oct 6, 2015 at 13:47
  • There is a method in Product model, getCategoryIds(), you can use that inside a foreach loop, and check for your IDs. Method returns an array of all category IDs, a product is associated to.
    – Prateek
    Oct 6, 2015 at 14:42
  • after that i marge into one category called toys but both boys and girls have same product 183 but girls have extra 100 product. How to find that 100 product
    – Magento 2
    Oct 6, 2015 at 14:49

1 Answer 1


Try this. Replace Boys Toys Id and Girls Toys Id with your respective Id.

This will return the products which are present in both boys & girls toys

$categoryId = array($boysToysId,$girlsToysId);
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
           ->addAttributeToFilter('category_id', array('in' => array('finset' => $categoryId)))
           ->having('count(*) = ' . count($categoryId));
  • Ok where i have to use this
    – Magento 2
    Oct 6, 2015 at 16:53
  • Where you want to see it?
    – vinothavn
    Oct 6, 2015 at 16:57
  • in back end i want marge
    – Magento 2
    Oct 6, 2015 at 16:58
  • So, you want to see it in the backend, right??
    – vinothavn
    Oct 6, 2015 at 16:59
  • yes if i merge back end it automatically show front end right
    – Magento 2
    Oct 6, 2015 at 17:03

Your Answer

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

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