9

I have an array of product ids now i want to get all categories where these products belongs in magento2.

For example

we have categories cat_1, cat_2 ,cat_3, cat_4, cat_5, cat_6, cat_7

product_id_1 exist in cat_1, cat_2

product_id_2 exist in cat_3, cat_4

product_id_3 exist in cat_1, cat_5

Now i want a collection of category having categories cat1, cat_2, cat_3, and cat_5

4 Answers 4

10

You can use array_merge to get all the ids from collection

$prodIds=$block->getProductCollection();
$catIds=[];         
foreach($prodIds as $pid){          
    $product = $this->_productloader->create()->load($pid);   
    $proCats = $product->getCategoryIds();    
    $catIds= array_merge($catIds, $pproCats);   
}
$finalCat = array_unique($catIds);
5

If you want to get all category ids based on the array of your product ids. You can get it as following way

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productIdsArray = array(1,2,3,4,5,6,7,8,9); // your product ids
$products = $objectManager->create("Magento\Catalog\Model\Product")->getCollection()->addAttributeToFilter('entity_id',array('in'=> $productIdsArray));

$allCategories = array();  // create a blank array
foreach ($products as $product) {
    $allCategories = array_merge($allCategories, $product->getCategoryIds());  // Merge product category ids array with $allCategories
}
$finalArray = array_unique($allCategories); // removes duplicate entries from an array

Note: Do not use ObjectManger directly in your phtml file. I recommend to use dependency injection.

4

You can always get the assigned categories of a product using following code.

In Constructor:

public function __construct(
        ...
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        ...
        $this->_productFactory = $productFactory;
        ...
    }

And in your method use following code

$product = $this->_productFactory->create()->load($pid); // $pid = Product_ID

$cats = $product->getCategoryIds(); // All Categories of $product you will get in array format eg. array(catid 1, catid 2, catid 3)
2
  • thanks, but i want to get category collection from multiple product ids
    – Waqar
    Oct 6, 2017 at 7:47
  • You can make looping of products and make an category array for all products! Hope it helps you Oct 6, 2017 at 9:15
3

You can get collection of category by Id in block like this:

namespace Your\Modules\Block;


protected $_categoryFactory;

public function __construct(
...
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
...
)
{
...
$this->_categoryFactory = $categoryFactory;
...
}
public function getCategory($categoryId)
{
    $category = $this->_categoryFactory->create();
    $category->load($categoryId);
    return $category;
}
public function getProductbyCategory($categoryId) 
{
    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('*');
    return $products;
}

Call function in your phtml file:

$collection = $block->getProductbyCategory($catId);
foreach($collection as $data){
    $data->getTitle();
}

Or you can define same like that in your helper and then call in phtml:

$yourHelper = $this->helper('Your\Module\Helper\Data');
$collection = $yourHelper->getProductbyCategory($catId);
foreach($collection as $data){
        $data->getTitle();
    }

Hope this help.

2
  • Thanks, I have an array of product ids and want to get there categories
    – Waqar
    Oct 6, 2017 at 8:00
  • your can try this: $product = $this->_productFactory->create()->load($pid); $product = $this->_productFactory->create()->load($pid); $cats = $product->getCategoryIds(); if(count($cats) ){ $firstCategoryId = $cats[0]; $_category = $this->_categoryFactory->create()->load($firstCategoryId); return $_category->getName(); } Oct 6, 2017 at 8:31

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.