I have loaded custom template before the product list in category list page.

this is my block file Vendor/Module/Block/Category.php

namespace Vendor\Module\Block;
class Category extends \Magento\Framework\View\Element\Template{
protected $categoryFactory; 
protected $_storeManager;    
protected $_categoryCollectionFactory;
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
    $this->categoryFactory = $categoryFactory;
    $this->_storeManager = $storeManager;
    $this->_categoryCollectionFactory = $categoryCollectionFactory;

 public function getParentCategoryList(){       
     $categoryId = 3; 
     $category = $this->categoryFactory->create()->load($categoryId);       
     $childrenCategories = $category->getChildrenCategories();
     return $childrenCategories;
 public function getChildCategoryList($categoryId){     
     $category = $this->categoryFactory->create()->load($categoryId);
     // Children  Categories
     $childrenCategories = $category->getChildrenCategories();
     return $childrenCategories;

This is my template file view/frontend/template/category.phtml

    $parentCategories = $block->getParentCategoryList();

  <div class="select-box">
   <div class="container">
    <div class="custom-dropdown">

       <select id="parent-cat" name="parent-cat"       

             <option value ="">- Please Select -</option>
             <?php foreach($parentCategories as $parent){
                    $parentCatName = $parent->getName();
                    $parentCatUrl = $parent->getUrl();
                    $parentCatId = $parent->getId();
                <option value="<?php echo $parentCatId; ?>"><?php echo 
            $parentCatName; ?></option>  
            <?php } ?>

    <div class="custom-dropdown center-cus-drop">
        <select id="child-cat" name="child-cat" 
            <option value ="">- Product category -</option>

    function getChildrenList(parentId) {
    if(parentId !=''){
        var customurl = "<?php echo $this- 
    >getUrl().'frontname/category/childcategory' ?>";
             url: customurl,
             type: "POST",
             data : 'category_id='+parentId,
             dataType: 'json',
             success : function(result) {                
                var optionHtml; 
                optionHtml +='<option value="">-Product Category-</option>';                
                for(var option of result){
                  optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';

function getChildUrl(childUrl) {
    var url = childUrl; 
      if (url) { 
          window.location = url; // redirect
      ///return false;


This is my controller file.

  use Magento\Catalog\Api\CategoryRepositoryInterface;
  use Magento\Catalog\Model\Category;
  use Magento\Framework\App\Action\Action;
  use Magento\Framework\App\Action\Context;
  use Magento\Framework\Controller\Result\JsonFactory;

 class Childcategory extends Action
   protected $categoryRepository;
   protected $resultJsonFactory;

  public function __construct(Context $context, CategoryRepositoryInterface $categoryRepository, JsonFactory $jsonFactory)
    $this->categoryRepository = $categoryRepository;
    $this->resultJsonFactory = $jsonFactory;

public function execute()
    $parentId = $this->getRequest()->getParam('category_id');     
    $category = $this->categoryRepository->get($parentId);
    $childrenData = [];

    foreach ($category->getChildrenCategories() as $childrenCategory) {
        $parent_category = $childrenCategory->getParentCategory();
        $childrenData[] = [
            'name' => $childrenCategory->getName(),
            'id' => $childrenCategory->getId(),
            'url'=> $childrenCategory->getUrl()
    return $this->resultJsonFactory->create()->setData($childrenData);

I have done first part, once the parent category is selected, i am populating the next drop down with its child categories,

On selecting the second drop down, i am redirecting to the child category url.

In this case as url redirects, the selected options again disappears.

How can i make both the drop down are selected on redirecting. Please anyone help me on this issue. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.