Jump to content

How to change the behavior of the ps_facetedsearch module? display all facets all the time


PrestaNewie

Recommended Posts

Hello Prestashop community,

By default, the ps_facetedsearch module displays the filter facets configured in the BackOffice. That's right.

Every time we select a new one, it starts narrowing down the results, hiding filters that no longer have matches.

My question is, How can I override the module so that the facets is not hidden? I would like the filters to add to each other.

Example.
I have two filters: blue products and red products.

I want to be able to select both and have it show me both the red and blue products.

Prestashop version 1.7.8.6

ps_facetedserch version 3.8.0

Thanks you!

Link to comment
Share on other sites

  • 1 year later...

Hello PrestaNewie,

 

You can change it in the module 'ps_facetedsearch' in the file 'modules/ps_facetedsearch/src/Filters/Block.php'. You will find a function that generates filter elements: 'getFilterBlock(...)'. This function calls multiple functions depending on the process, such as attributes, features, etc.

In my situation, I needed to display all 'Features' filter elements in form, so I modified the 'getFeaturesBlock' function as follows:

I commented this line // $results = $filteredSearchAdapter->valueCount('id_feature_value'); and created a new SQL query to retrieve all enabled features for facets. You can override the facets process by disabling the native "$filteredSearchAdapter->valueCount(...)" and replacing it with the new query containing all elements. Similarly, you can do the same for attributes or manufacturers."

see my exemple with getFeaturesBlock

 

    /**
     * Get the features filter block
     *
     * @param array $filter
     * @param array $selectedFilters
     * @param int $idLang
     *
     * @return array
     */
    private function getFeaturesBlock($filter, $selectedFilters, $idLang)
    {
        $featureBlock = [];
        $idFeature = $filter['id_value'];
        $filteredSearchAdapter = null;
        if (!empty($selectedFilters['id_feature'])) {
            foreach ($selectedFilters['id_feature'] as $key => $selectedFilter) {
                if ($key == $idFeature) {
                    $filteredSearchAdapter = $this->searchAdapter->getFilteredSearchAdapter('with_features_' . $idFeature);

                    break;
                }
            }
        }

        if (!$filteredSearchAdapter) {
            $filteredSearchAdapter = $this->searchAdapter->getFilteredSearchAdapter();
        }

        $features = $this->dataAccessor->getFeatures($idLang);
        if (empty($features)) {
            return [];
        }

        $filteredSearchAdapter->addOperationsFilter(
            'id_feature_' . $idFeature,
            [[['id_feature', [(int) $idFeature]]]]
        );

        $filteredSearchAdapter->addSelectField('id_feature');
//        $results = $filteredSearchAdapter->valueCount('id_feature_value');
        $sql = 'SELECT DISTINCT f.id_feature, fv.id_feature_value, 1 AS c FROM feature f LEFT JOIN feature_value fv ON f.id_feature = fv.id_feature where f.id_feature = '.($idFeature);
        $results = Db::getInstance()->executeS($sql);


        foreach ($results as $key => $values) {
            $idFeatureValue = $values['id_feature_value'];
            $idFeature = $values['id_feature'];
            $count = $values['c'];

            $feature = $features[$idFeature];

            if (!isset($featureBlock[$idFeature])) {
                $features[$idFeature]['featureValues'] = $this->dataAccessor->getFeatureValues($idFeature, $idLang);

                $featureBlock[$idFeature] = [
                    'type_lite' => 'id_feature',
                    'type' => 'id_feature',
                    'id_key' => $idFeature,
                    'values' => [],
                    'name' => $feature['name'],
                    'url_name' => $feature['url_name'],
                    'meta_title' => $feature['meta_title'],
                    'filter_show_limit' => (int) $filter['filter_show_limit'],
                    'filter_type' => $filter['filter_type'],
                ];
            }

            $featureValues = $features[$idFeature]['featureValues'];
            if (!isset($featureValues[$idFeatureValue]['value'])) {
                continue;
            }

            $featureBlock[$idFeature]['values'][$idFeatureValue] = [
                'nbr' => $count,
                'name' => $featureValues[$idFeatureValue]['value'],
                'url_name' => $featureValues[$idFeatureValue]['url_name'],
                'meta_title' => $featureValues[$idFeatureValue]['meta_title'],
            ];

            if (array_key_exists('id_feature', $selectedFilters)) {
                foreach ($selectedFilters['id_feature'] as $selectedFeature) {
                    if (in_array($idFeatureValue, $selectedFeature)) {
                        $featureBlock[$feature['id_feature']]['values'][$idFeatureValue]['checked'] = true;
                    }
                }
            }
        }


        $featureBlock = $this->sortFeatureBlock($featureBlock);

        return $featureBlock;
    }

 

sssscccccccccccc.thumb.png.b376d4642f7da52966e4b88e18af6d37.png

Edited by Maamria (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...