2

I want to hide price filter on some of my category pages. enter image description here

Can anyone tell me how can I achieve this?

Thanks In Advance..!

4 Answers 4

1

You can try using following code. I have created a plugin in custom module.

app/code/Anshu/HideFilters/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Anshu_HideFilters',
    __DIR__
);

app/code/Anshu/HideFilters/etc/module.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_HideFilters">
        <sequence>
            <module name="Magento_Catalog" />
        </sequence>
    </module>
</config>

app/code/Anshu/HideFilters/etc/di.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer\FilterList">
        <plugin name="hideFilters_plugin" type="Anshu\HideFilters\Plugin\FilterPlugin" />
    </type>
</config>

app/code/Anshu/HideFilters/Plugin/FilterPlugin.php

<?php
declare(strict_types=1);

namespace Anshu\HideFilters\Plugin;

use Magento\Catalog\Model\Layer\FilterList;
use Magento\Framework\App\Request\Http;

class FilterPlugin
{
    /**
     * @var Http
     */
    private $request;

    public function __construct(
        Http $request
    ) {
        $this->request = $request;
    }

    public function afterGetFilters(FilterList $subject, $result)
    {
        $catId = $this->request->getParam('id');
        if($catId != '21') {
            return $result;
        }
        $count = 0;
        foreach ($result as $r){
            if($r->getName() == 'Price'){
                unset($result[$count]);
                return $result;
            }
            $count++;
        }
    }
}

Here I am hiding Price filter for category with ID 21.

Modify it according to your requirement.

1
0

Navigate to the following file

app/design/frontend/vendor/your_theme/Magento_LayeredNavigation/templates/layer/view.phtml

Add your logic around the foreach function of $block->getFilters() as shown below

<?php
    // create an array of the category ids in which you don't want the price filter
    $restricted_categories = [3, 4, 10, 14];
    $currentCategoryId = $block->getLayer()->getCurrentCategory()->getId();
?>
.....
.....
.....
<?php foreach ($block->getFilters() as $filter): ?>
    <?php
        if (in_array($currentCategoryId, $restricted_categories) && $filter->getRequestVar() == 'price') {
            continue;
        }
    ?>
    .....
    .....
    .....
<?php endforeach; ?>

Note: It seems that you are using any third party extension for Layered Navigation. If so, you have to use this code in the file of your extension.

0

Can hide Filters with minimum modify in PHTML Theme template.


  1. Add XML instruction to Category Edit Page - field Layout Update XML
<referenceContainer name="sidebar.main">
    <referenceBlock name="catalog.leftnav">
        <action method="assign">
            <argument name="key" xsi:type="string">filterhide</argument>
            <argument name="value" xsi:type="array">
                <item name="filter1" xsi:type="string">filter1</item>
                <item name="filter2" xsi:type="string">filter2</item>
                <item name="filter3" xsi:type="string">filter3</item>
                <item name="filter4" xsi:type="string">filter4</item>
                <item name="filter5" xsi:type="string">filter5</item>
            </argument>
         </action>
    </referenceBlock>
</referenceContainer>
  1. Update PHTML theme template Edit Theme file Magento_LayeredNavigation::layer/view.phtml Add following code:
<?php
$filterhideVar = false;
if (isset($filterhide) && is_array($filterhide)) {
    $sfilterhideVar = $filterhide;
}
?>

And for hide filter add: After construction
<?php foreach ($block->getFilters() as $filter) : ?>

Add following piece of PHP code:

 if (isset($filterhideVar) &&
 isset($filterhideVar[$filter>getAttributeModel()->getAttributeCode()]))
 {continue;}

Same method can pass custom vars from Adminpanel to Phtml.

-1

I think you can try using this code

app/design/frontend/YOURPACKAGE/YOURTEMPLATE>template>catalog/layer/view.phtml

  <?php if($_filter->getItemsCount()):?>
  <?php $show=1;?>
    <!-- curern filter price and currentc Cat match -->
  <?php if($_filter->getFilter()->getRequestVar()!=='price'){?>
    <?php if(Mage::registry('current_category') &&(Mage::registry('current_category')->getId()==$catId)):
    $show=0;
        endif;
     ?>
  <?php } ?>
  <!-- check show 
  <?php if($show==1){?>
  <?php echo $_filter->getHtml() ?>
  <?php } ?>
  <?php endif;?>

Or you can also refer to this link

1
  • OP has tagged this with Magento 2 and this appears to be an answer for Magento 2
    – Barry
    Oct 21, 2019 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.