Jump to content

Show 1 certain feature value in product.tpl


andreassoegaard

Recommended Posts

Hello all Presta-experts!

 

I'm searching for a way to show 1 certain feature value in product.tpl.

 

I tried using this code: 

 

{foreach $product.features as $feature}
{if $feature.name == 'Normalpris'}<p>{$feature.value|escape:'htmlall':'UTF-8'}</p>{/if}
{/foreach}
 
and by ID
 
{foreach $product.features as $feature}
{if $feature.id == 8}<p>{$feature.value|escape:'htmlall':'UTF-8'}</p>{/if}
{/foreach}
 
But it returns nothing. I have already searched through 15+ topics, but none of the solutions worked out for me.
 
If it means any special then the feature has a custom value.
 
It is Presta 1.5.6.1.
 
Any suggestions?
 
Thanks!!

 

Link to comment
Share on other sites

  • 4 months later...
  • 2 months later...

this solution worked for me

for product-list.tpl i used $product.features 

for product.tpl use $features

 

Using this it looks for features matching "Pack Info" and "Pack Size" then saves those values into $packinfo and $packsize

 

 

{if isset($product.features)}
<!-- Data sheet -->
{foreach from=$product.features item=feature}
{if $feature.name == 'Pack Info'}
{if isset($feature.value)}
{$packinfo=$feature.value}
{else}
{$packinfo = 'No pack info set'}
{/if}
{/if}
{if $feature.name == 'Pack Size'}
{if isset($feature.value)}
{$packsize = $feature.value}
{else}
{$packsize = 1}
{/if}
{/if}
{/foreach}
<!--end Data sheet -->
{/if}
 
{if !isset($packinfo)}
{$packinfo = 'No pack info set'}
{/if}
{if !isset($packsize)}
{$packsize = 1}
{/if}
 
--- 
 
{if isset($features) && $features}
<!-- Data sheet -->
{foreach from=$features item=feature}
{if $feature.name == 'Pack Info'}
{$packinfo=$feature.value}
{/if}
{if $feature.name == 'Pack Size'}
{$packsize=$feature.value}
{/if}
{/foreach}
<!--end Data sheet -->
{/if}
{if !isset($packinfo)}
{$packinfo = 'No pack info set'}
{/if}
{if !isset($packsize)}
{$packsize = 1}
{/if}
Edited by zeki893 (see edit history)
Link to comment
Share on other sites

  • 1 year later...
Pour afficher une seule caractéristique sur la page produit

Dans la fiche produit, les caractéristiques sont stockées dans un tableau. Pour afficher une seule caractéristique, il n’est pas possible d’accéder directement à une caractéristique avec son id.

 

Dans la fonction initContent de l’override du product controller, créons un nouveau tableau.

/* Feature array by id */
      $features = $this->product->getFrontFeatures($this->context->language->id);
      foreach ($features as $key=>$value) {
        $features_by_key[$value['id_feature']]['name'] = $value['name'];
        $features_by_key[$value['id_feature']]['value'] = $value['value'];
      }

puis on l’assigne :

$this->context->smarty->assign(array(
				'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
				'customizationFields' => ($this->product->customizable) ? $this->product->getCustomizationFields($this->context->language->id) : false,
				'accessories' => $this->product->getAccessories($this->context->language->id),
				'return_link' => $return_link,
				'product' => $this->product,
				'product_manufacturer' => $product_manufacturer,
				'token' => Tools::getToken(false),
				'features' => $features,
        'features_by_key' => $features_by_key,

Dans le product.tpl, pour afficher la caractéristque avec l’id 3 :

{if isset($features_by_key.3.value) & $features_by_key.3.value}
{$features_by_key.3.name} : {$features_by_key.3.value}
{/if}
Link to comment
Share on other sites

  • 1 month later...

Hi adolphemattos! Your code was very helpful for me thanks! Can you please help me?

I need to use Category Name and Feature Value variables in a Product description. I added code to ProductController.php as described here
for CMS page (thanks vekia)

 

My code in ProductController.php:

if (isset($this->product)){
    $this->product->description=str_replace("{MY_VARIABLE}",($this->category->name),"{$this->product->description}");
}

It work good and now I can put {MY_VARIABLE} to product description (backend html editor) and it will show default category name.

 

As for Feature Values, I made everything as you described in your previous post, now I can put {$features_by_key.3.value} to .tpl and it will show feature value with needed id. But how I can put Feature Variable to html Product Description? Will it possible to do it by same way as I did it for Category name? If yes, could you please help me to create new code to put it to ProductController.php?

 

PS. I'm not coder and tried to play with your code and with no luck, now it make me crazy...  please help me if you can )

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

Answer to my question :)

If you want to use certain feature as variable in you product description you need to put code to ProductController.php as described Adolphemattos in post #6 and then put this code:

if (isset($this->product)){
$feature_var = $features_by_key[1]['value'];
    $this->product->description=str_replace("{FEATURE_VAR}","{$feature_var}","{$this->product->description}");
}

right before this line:

        $this->setTemplate(_PS_THEME_DIR_.'product.tpl');

now you can write {FEATURE_VAR} in product description and in front office it will show feature with id1.

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

  • 3 years later...

Hi there Is there a way to call the feature of level two by id ? when I used for example

{if $feature.id_feature == '24'} -> it's working

{if $feature.id_feature == '123'} -> it's not working

(this two have the same $product->id_category_default)

And do you know how to remove a feature_id list of product ? I'm new to this language so I don't have the grammar :)

For example {if $product->id_category_default && [exclude ??] $feature.id_feature == '24'} ??

Thanks a lot,

Clément

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...