Jump to content

Un-group Features


Recommended Posts

Hi,

In product-details.tpl the features,  when grouped, are generated inside a single <dd>

  {block name='product_features'}
    {if $product.grouped_features}
      <section>
        <h3>{l s='Data sheet' d='Shop.Theme.Catalog'}</h3>
        <dl>
          {foreach from=$product.grouped_features item=feature}
            <dt>{$feature.name}</dt>
            <dd>{$feature.value|escape:'htmlall'|nl2br nofilter}</dd>
          {/foreach}
        </dl>
      </section>
    {/if}
  {/block}

I need to create somethink like the following (that of course doesn't work):

 

    {if $product.grouped_features}
      <section>
        <h3>{l s='Data sheet' d='Shop.Theme.Catalog'}</h3>
        <dl>
          {foreach from=$product.grouped_features item=feature}
            <dt>{$feature.name}</dt>
            <dd>{foreach $feature.value}<span title="{$feature.value|replace:' ':'_'}">{$feature.value|escape:'htmlall'|nl2br nofilter}</span>{/foreach}</dd>
          {/foreach}
        </dl>
      </section>
    {/if}

 

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

  • 1 year later...
  • 1 year later...

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 :)

Edited by AfterGlow93 (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...