1

I am trying to implement this feature: the filters that are shown in the search results page are only visible if every products in that results page have the attribute linked with the filter populated.

For instance, a search result have 2 products:

  • product A with attribute Attribute1 and Attribute2 both populated
  • Product B with only the Attribute1 populated

I would like that the filter shown in the search result is only the filter of the Attribute1 (because the product B doesn't have the Attribute2 populated).

I already did this with the layered navigation on categories, by modifying the following file:

/var/www/html/magento/vendor/magento/module-layered-navigation/view/frontend/templates/layer/view.phtm

in the following way:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
/**
 * Category layered navigation
 *
 * @var $block \Magento\LayeredNavigation\Block\Navigation
 */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
?>



<?php if ($block->canShowBlock()) : ?>
    <div class="block filter">
        <div class="block-title filter-title">
            <strong><?= $block->escapeHtml(__('Shop By')) ?></strong>
        </div>

        <div class="block-content filter-content">
            <?= $block->getChildHtml('state') ?>

            <?php if ($block->getLayer()->getState()->getFilters()) : ?>
                <div class="block-actions filter-actions">
                    <a href="<?= $block->escapeUrl($block->getClearUrl()) ?>" class="action clear filter-clear"><span><?= $block->escapeHtml(__('Clear All')) ?></span></a>
                </div>
            <?php endif; ?>
            <?php $wrapOptions = false; ?>
            <?php foreach ($block->getFilters() as $filter) : ?>
                <?php if (!$wrapOptions) : ?>
                    <strong role="heading" aria-level="2" class="block-subtitle filter-subtitle"><?= $block->escapeHtml(__('Shopping Options')) ?></strong>
                    <dl class="filter-options" id="narrow-by-list">
                    <?php $wrapOptions = true;
                endif; ?>
                    <?php if ($filter->getItemsCount()>1) : ?>

<?php  if($filter->getName() != __('Category')) : ?>

        <?php if ($category->getProductCollection()->count() == $category->getProductCollection() ->addAttributeToSelect('sku') ->addAttributeToFilter($block->escapeHtml(__($filter->getRequestVar())),array('notnull'=>true))->getSize()) : ?>
                        <dt role="heading" aria-level="3" class="filter-options-title"><?= $block->escapeHtml(__($filter->getName())) ?></dt>
                        <dd class="filter-options-content"><?= /* @noEscape */ $block->getChildBlock('renderer')->render($filter) ?></dd>
    <?php endif; ?>
<?php  else: ?>

        <dt role="heading" aria-level="3" class="filter-options-title"><?= $block->escapeHtml(__($filter->getName())) ?></dt>
                        <dd class="filter-options-content"><?= /* @noEscape */ $block->getChildBlock('renderer')->render($filter) ?></dd>
<?php endif; ?>
<?php endif; ?>
            <?php endforeach; ?>
            <?php if ($wrapOptions) : ?>
                </dl>
            <?php endif; ?>
        </div>
    </div>
<?php endif; ?>

But I cannot do the same with the search result. Can please somebody help me?

Thank you

1 Answer 1

0

I found the solution, below the code:

  1 <?php
  2 /**
  3  * Copyright © Magento, Inc. All rights reserved.
  4  * See COPYING.txt for license details.
  5  */
  6 ?>
  7 <?php
  8 /**
  9  * Category layered navigation
 10  *
 11  * @var $block \Magento\LayeredNavigation\Block\Navigation
 12  */
 13 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 14 $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
 15 
 16 $layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
 17 
 18 ?>
 19 
 20 
 21 
 22 <?php if ($block->canShowBlock()) : ?>
 23     <div class="block filter">
 24         <div class="block-title filter-title">
 25             <strong><?= $block->escapeHtml(__('Shop By')) ?></strong>
 26         </div>
 27 
 28         <div class="block-content filter-content">
 29             <?= $block->getChildHtml('state') ?>
 30 
 31             <?php if ($block->getLayer()->getState()->getFilters()) : ?>
 32                 <div class="block-actions filter-actions">
 33                     <a href="<?= $block->escapeUrl($block->getClearUrl()) ?>" class="action clear filter-clear"><span><?= $block->escapeHtml(__('Clear All')) ?></span></a>
 34                 </div>
 35             <?php endif; ?>
 36             <?php $wrapOptions = false; ?>
 37             <?php foreach ($block->getFilters() as $filter) : ?>
 38                 <?php if (!$wrapOptions) : ?>
 39                     <strong role="heading" aria-level="2" class="block-subtitle filter-subtitle"><?= $block->escapeHtml(__('Shopping Options')) ?></strong>
 40                     <dl class="filter-options" id="narrow-by-list">
 41                     <?php $wrapOptions = true;
 42                 endif; ?>
 43                     <?php if ($filter->getItemsCount()>1) : ?>
 44 
 45 <?php  if($filter->getName() != __('Category')) : ?>
 46 
 47 <?php if ($category) : ?>
 48 
 49         <?php if ($category->getProductCollection()->count() == $category->getProductCollection() ->addAttributeToSelect('sku') ->addAttributeToFilter($block->escapeHtml(__($filter->getRequestVar())),array('notnull'=>true))->getSize()) : ?>
 50                         <dt role="heading" aria-level="3" class="filter-options-title"><?= $block->escapeHtml(__($filter->getName())) ?></dt>
 51                         <dd class="filter-options-content"><?= /* @noEscape */ $block->getChildBlock('renderer')->render($filter) ?></dd>
 52     <?php endif; ?>
 53 
 54 <?php else: ?>
 55 
 56 <?php
 57 
 58 echo "ciao";
 59 $prodottiora = $block->getResultCount();
 60 echo $prodottiora;
 61 
 62 echo $block->getLayer()->getProductCollection()->count();
 63 
 64 
 65 echo $block->getLayer()->getProductCollection()->addAttributeToSelect('sku')->addAttributeToFilter($block->escapeHtml(__($filter->getRequestVar())),array('notnull'=>true))->getSize();
 66 
 67 ?>
 68 
 69 <?php if ($block->getLayer()->getProductCollection()->count() == $block->getLayer()->getProductCollection()->addAttributeToSelect('sku')->addAttributeToFilter($block->escapeHtml(__($filter->getRequestVar())),array('notnull'=>true))->getSize()) : ?>
 70                         <dt role="heading" aria-level="3" class="filter-options-title"><?= $block->escapeHtml(__($filter->getName())) ?></dt>
 71                         <dd class="filter-options-content"><?= /* @noEscape */ $block->getChildBlock('renderer')->render($filter) ?></dd>
 72     <?php endif; ?>
 73 
 74 
 75 
 76 
 77 
 78 
 79 <?php endif; ?>
 80 
 81 <?php  else: ?>
 82 
 83 
 84         <dt role="heading" aria-level="3" class="filter-options-title"><?= $block->escapeHtml(__($filter->getName())) ?></dt>
 85                         <dd class="filter-options-content"><?= /* @noEscape */ $block->getChildBlock('renderer')->render($filter) ?></dd>
 86 <?php endif; ?>
 87 <?php endif; ?>
 88             <?php endforeach; ?>
 89             <?php if ($wrapOptions) : ?>
 90                 </dl>
 91             <?php endif; ?>
 92         </div>
 93     </div>
 94 <?php endif; ?>

Your Answer

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

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