Jump to content

Edit History

Houssem MAAMRIA

Houssem MAAMRIA

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

Houssem MAAMRIA

Houssem MAAMRIA

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 change the 'Features' filter elements, 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

×
×
  • Create New...