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.

  • which code you use for get category please declare it here so i can help you – Deexit Sanghani Mar 11 '17 at 6:34
  • I have product collection and need only categories ids from product collection by store. – Prince Patel Mar 11 '17 at 6:44
5

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();
}

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.