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. – Rahul Kumar Das Oct 6 '15 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 '15 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 '15 at 14:49

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));
|improve this answer|||||
  • Ok where i have to use this – Magento 2 Oct 6 '15 at 16:53
  • Where you want to see it? – vinothavn Oct 6 '15 at 16:57
  • in back end i want marge – Magento 2 Oct 6 '15 at 16:58
  • So, you want to see it in the backend, right?? – vinothavn Oct 6 '15 at 16:59
  • yes if i merge back end it automatically show front end right – Magento 2 Oct 6 '15 at 17:03

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.