Here is the code for getting category by category URL key.
The module name is: Magemonkeys_Categorydata
Create a block file: Categoryurl.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace Magemonkeys\Categorydata\Block\Index; class Categoryurl extends \Magento\Framework\View\Element\Template { { protected $filter; public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, array $data = [] ) { parent::__construct($context, $data); $this->categoryFactory = $categoryFactory; } public function getCategory($urlKey) { $categories = $this->categoryFactory->create()->getCollection() ->addAttributeToFilter('url_key', $urlKey) ->addAttributeToSelect(['entity_id']); return $categories; } } |
Now you can get category by using this function getCategory($urlKey) in your phtml file
1 2 3 |
$urlKey = 'bags'; $category = $this->getCategory($urlKey); print_r($category->getData()); |
You can get an array of category data.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62846cc62a372643501286/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...