How to get category by url key in Magento 2
Here is the sample code to get category by category url key. <?php namespace Mageprince\Test\Model; class SampleClass { protected $filter; public function __construct( … \Magento\Catalog\Model\CategoryFactory $categoryFactory, … ) { $this–>categoryFactory = $categoryFactory; } public function getCategory($urlKey, $parentCatId) { $categories = $this–>categoryFactory->create()–>getCollection() –>addAttributeToFilter(‘url_key’, $urlKey) –>addAttributeToSelect([‘entity_id’]); return $categories; } } Now…
Read More