2

I want to get category ids of specific store.

$product->getCategoryIds()

This will get all categories of product

Array ( [0] => 63 [1] => 178 )

63 is category of store 1 and 178 is category of store 2 but I want only category 63.

3
  • which code you use for get category please declare it here so i can help you Commented Mar 11, 2017 at 6:34
  • I have product collection and need only categories ids from product collection by store. Commented Mar 11, 2017 at 6:44
  • @PrincePatel did u get solution for this. i am also looking for same thing
    – Nafsss
    Commented Jun 19, 2020 at 11:33

1 Answer 1

6

use \Magento\Store\Model\StoreManagerInterface for current store and apply it in your query

Try this

protected $storeManager;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    $data = []
) {
    $this->storeManager = $storeManager;
    parent::__construct($data);
}

$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*')
    ->setStore($this->storeManager->getStore());

foreach ($categories as $category){
    $category->getId();
}
3
  • not working in multisite Commented Nov 28, 2019 at 10:18
  • That's intended, the OP asked for a specific store. Commented Apr 25, 2020 at 7:13
  • @ManishMaheshwari I have facing same issue in multisite for get store category. what solution have you applied ? Commented Oct 13, 2022 at 9:04

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.