0

I have a category 1 which belongs to store A and category 2 which belongs to store B. When I am in store A and run the query $category->getStoreId(), both categories returns A, and when I am in store B, both categories return B.

.....
\Magento\Catalog\Model\CategoryRepository $categoryRepository,
......

$categoryIds = $product->getCategoryIds();

foreach($categoryIds as $categoryId) {
   $category = $categoryRepository->get($categoryId);
   echo $category->getStoreId();
} 

I tried everything using categorycollection, categoryfactory, and categoryrepository. How do I get the store id of that category instead of current store id. FYI, setStore, setProductStoreId(), addStoreFilter etc, none of them worked

2 Answers 2

0

This is due to the fact that the store category do not exist on the other store. Actually it would have been more expected that it returns a null category.

This is mostly due to the fact that repositories will use the flat data if i'm not wrong. And the flat data won't be mapped as the category really do not exist in your store.

I would say this behaviour is normal what you are trying to achieve is a little out of the box; but not really uncommun. On my side when this happens i'm used to directly sql query database with a simple connection object depending on the information i need. This way you don't have to pass by the flat data and you can get what you need.

Not sure it's the best practice though...

2
  • I did not understand your answer. Each category exist in their respective store. They should return their own store ids, however, both returns same value as current store when I switch store. For eg, both return store A when am in store A or both return store B when am in store B, but in actual one has set storeid A and the other has set storeid B
    – Siri
    Commented Apr 17, 2023 at 10:47
  • You should add more code then like how you get the product and how you are initializing your constructor. If categ A1 exist in store A and do not exist in store B and categ B1 exist in store B and do not exist in store A; then what would make sense is that in store A the categ B1 is null and in store B the categ A1 is null. It's one thing to get all products categeries ids. It's an other thing to get the category information if that specified category do not exist in the store.
    – Claims
    Commented Apr 17, 2023 at 12:47
0

After strenuous research, following has helped me achieve only specific store categories in multistore site.

$categoryIds = $product->getCategoryIds();
$collection = $this->categoryCollectionFactory->create()
            ->addAttributeToSelect(self::ATTRIBUTE_CODE)
            ->joinUrlRewrite()
            ->addFilter('store_id', $storeId)
            ->addAttributeToFilter('entity_id', $categoryIds);

joinurlRewrite and store_id filter are the key things here.

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.