3

I want to filter category by store id. Used the following code, but it is not working.

$collection = $this->categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*')
        ->setStoreId(0);

4 Answers 4

1
$collection = $this->categoryCollectionFactory->create();
    $collection->addAttributeToSelect('*')
    ->setStore($storeId);

instead ->setStoreId(0); use ->setStore(0);

0
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

    $categories = $categoryCollection->create();
    $categories->addAttributeToSelect('*');
    $categories->setStore('0'); /* set here your store id */

    foreach ($categories as $category) {
        echo $category->getName() . '<br />';
    }
0

Pretty valid question with no valid answers so far. Hopefully my answer will help you to solve the issue. In fact, "setStore" or "setStoreId" did nothing for me, still current store ID was used.

Here's a workaround for this that I've found as the easiest:

$currentStoreId = $this->storeManager->getStore()->getId();
$this->storeManager->setCurrentStore(2);
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect('name');
$cats = $collection->getColumnValues('name');
// Don't forget to bring back the initial store ID
$this->storeManager->setCurrentStore($currentStoreId);
-2

Try below code. this is working for me.

$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');        
$collection->addStoreFilter($this->_storeManager->getStore($store_id));
1
  • There's no "addStoreFilter" method in category collection Commented Sep 1, 2022 at 17:32

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.