Jump to content

Edit History

AfterGlow93

AfterGlow93

Hi :)

Sorry to bring up this old post but lack of answer to this case could help some people here.

For anyone looking for more informations about way to un-group or change delimiter of grouped features :

Variable $products.grouped_features is built through ProductLazyArray class, by the function buildGroupedFeatures then called by the function getGroupedFeatures.

Here is the function buildGroupedFeatures (Line 1174 of PrestaShop/src/Adapter/Presenter/Product/ProductLazyArray.php

 protected function buildGroupedFeatures(array $productFeatures)
    {
        $valuesByFeatureName = [];
        $groupedFeatures = [];

        // features can either be "raw" (id_feature, id_product_id_feature_value)
        // or "full" (id_feature, name, value)
        // grouping can only be performed if they are "full"
        if (empty($productFeatures) || !array_key_exists('name', reset($productFeatures))) {
            return [];
        }

        foreach ($productFeatures as $feature) {
            $featureName = $feature['name'];
            // build an array of unique features
            $groupedFeatures[$featureName] = $feature;
            // aggregate feature values separately
            $valuesByFeatureName[$featureName][] = $feature['value'];
        }

        // replace value from features that have multiple values with the ones we aggregated earlier
        foreach ($valuesByFeatureName as $featureName => $values) {
            if (count($values) > 1) {
                sort($values, SORT_NATURAL);
                $groupedFeatures[$featureName]['value'] = implode("\n", $values);
            }
        }

        return $groupedFeatures;
    }

The implode can be replaced with another delimiter (I used a semicolon with spaces in my case) or set in an array with an unique id which will be called into a foreach loop.

Hope this helps :)

AfterGlow93

AfterGlow93

Hi :)

Sorry to bring up this old post but lack of answer to this case could help some people here.

For anyone looking for more informations about way to un-group or change delimiter of grouped features :

Variable $products.grouped_features is built through ProductLazyArray class, by the function getGroupedFeatures.

Here is the function (Line 1174 of PrestaShop/src/Adapter/Presenter/Product/ProductLazyArray.php

 protected function buildGroupedFeatures(array $productFeatures)
    {
        $valuesByFeatureName = [];
        $groupedFeatures = [];

        // features can either be "raw" (id_feature, id_product_id_feature_value)
        // or "full" (id_feature, name, value)
        // grouping can only be performed if they are "full"
        if (empty($productFeatures) || !array_key_exists('name', reset($productFeatures))) {
            return [];
        }

        foreach ($productFeatures as $feature) {
            $featureName = $feature['name'];
            // build an array of unique features
            $groupedFeatures[$featureName] = $feature;
            // aggregate feature values separately
            $valuesByFeatureName[$featureName][] = $feature['value'];
        }

        // replace value from features that have multiple values with the ones we aggregated earlier
        foreach ($valuesByFeatureName as $featureName => $values) {
            if (count($values) > 1) {
                sort($values, SORT_NATURAL);
                $groupedFeatures[$featureName]['value'] = implode("\n", $values);
            }
        }

        return $groupedFeatures;
    }

The implode can be replaced with another delimiter (I used a semicolon with spaces in my case) or set in an array with an unique id which will be called into a foreach loop.

Hope this helps :)

AfterGlow93

AfterGlow93

Hi :)

Sorry to bring up this old post but lack of answer to this case could help some people here.

For anyone looking for more informations about way to un-group or change delimiter of grouped features :

Variable $products.grouped_features is built through ProductLazyArray class, by the function to getGroupedFeatures.

Here is the function (Line 1174 of PrestaShop/src/Adapter/Presenter/Product/ProductLazyArray.php

 protected function buildGroupedFeatures(array $productFeatures)
    {
        $valuesByFeatureName = [];
        $groupedFeatures = [];

        // features can either be "raw" (id_feature, id_product_id_feature_value)
        // or "full" (id_feature, name, value)
        // grouping can only be performed if they are "full"
        if (empty($productFeatures) || !array_key_exists('name', reset($productFeatures))) {
            return [];
        }

        foreach ($productFeatures as $feature) {
            $featureName = $feature['name'];
            // build an array of unique features
            $groupedFeatures[$featureName] = $feature;
            // aggregate feature values separately
            $valuesByFeatureName[$featureName][] = $feature['value'];
        }

        // replace value from features that have multiple values with the ones we aggregated earlier
        foreach ($valuesByFeatureName as $featureName => $values) {
            if (count($values) > 1) {
                sort($values, SORT_NATURAL);
                $groupedFeatures[$featureName]['value'] = implode("\n", $values);
            }
        }

        return $groupedFeatures;
    }

The implode can be replaced with another delimiter (I used a semicolon with spaces in my case) or set in an array with an unique id which will be called into a foreach loop.

Hope this helps :)

×
×
  • Create New...