Last active
July 24, 2019 07:02
-
-
Save ravibhoraniya/3f6a8ebbcdd7647f23b6a49bdeec7df1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. To add this, you will need to override the following files: | |
vendor/magento/core/module-catalog-search/Block/Advanced/Form.php | |
vendor/magento/core/module-catalog-search/Model/Advanced.php | |
vendor/magento/module-catalog-search/view/frontend/templates/advanced/form.phtml | |
2. Form.php | |
protected $_categoryHelper; | |
public function __construct( | |
\Magento\Catalog\Helper\Category $categoryHelper | |
){ | |
$this->_categoryHelper = $categoryHelper; | |
} | |
public function getStoreCategories() | |
{ | |
return $this->_categoryHelper->getStoreCategories(); | |
} | |
3. Advanced.php - Replace the getSearchCriterias() function with the code below: | |
public function getSearchCriterias() | |
{ | |
$search = $this->_searchCriterias; | |
/* display category filtering criteria */ | |
if(isset($_GET['cat']) && is_numeric($_GET['cat'])) { | |
$category = $this->_objectManager->get('Magento\Catalog\Model\Category')->load($_GET['cat']); | |
$search[] = array('name'=>'cat','value'=>$category->getName()); | |
} | |
return $search; | |
} | |
public function getProductCollection() | |
{ | |
if ($this->_productCollection === null) { | |
$this->_productCollection = $this->_objectManager->get('Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection') | |
->addAttributeToSelect($this->_objectManager->get('Magento\Catalog\Model\Config')->getProductAttributes()) | |
->addMinimalPrice() | |
->addStoreFilter(); | |
/* need to include product active and visibility filtering here*/ | |
/* include category filtering */ | |
if(isset($_GET['cat']) && is_numeric($_GET['cat'])) $this->_productCollection->addCategoryFilter($this->_objectManager->get('Magento\Catalog\Model\Category')->load($_GET['cat']),true); | |
} | |
return $this->_productCollection; | |
} | |
4. form.phtml | |
<li> | |
<label for="category_search_field">Search by Category:</label> | |
<select name="cat" id="category_search_field"> | |
<option value="">-- Any Category --</option> | |
<?php foreach ($this->getStoreCategories() as $_category): ?> | |
<?php if($_category->hasChildren()): ?> | |
<option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option> | |
<?php if($_category->getIsActive()) : ?> | |
<option value="<?php echo $_category->getId(); ?>"<?php echo ($this->getRequest()->getQuery('cat') == $_category->getId() ? ' selected="selected"': "") ?>><?php echo $_category->getName(); ?></option> | |
<?php endif; ?> | |
<?php elseif($_category->getIsActive()): ?> | |
<option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option> | |
<?php endif; ?> | |
<?php endforeach ?> | |
</select> | |
</li> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment