0

I am having the same category in 2 stores and on the custom page, I need category detail as per current store id. I am using the below code to get a category by using the category URL.

In block

public function getCategoryId()

{
    $pfCategoryId = $this->categoryFactory->create()
        ->getCollection()
        ->addAttributeToFilter('url_key', 'category-url')
        ->addAttributeToSelect(['entity_id']);
    return $pfCategoryId;
}

In phtml

$categoryId = $block->getCategoryId()->getFirstItem()->getEntityId();

Now I am having 2 stores. How to get store specific category id by using the category URL?

1 Answer 1

0

following code will help you to get category collection by url key and store code.

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


$pfCategoryId = $this->categoryFactory->create()
    ->getCollection()
    ->addAttributeToFilter('url_key', 'category-url')
    ->setStore($this->_storeManager->getStore())
    ->addAttributeToSelect(['entity_id']);
return $pfCategoryId;

If this help you then hit like.

Thank you,
Hiren Patel

1
  • Hi Hiren, Thank you for your response. I have used the above code but no luck.
    – Payal
    Oct 22, 2020 at 5:10

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.