Jump to content

add product features or combinations to the product-list.tpl


Recommended Posts

  • 2 weeks later...
  • 2 weeks later...

not so simple, category.php (which calls products-list.tpl) does not query for attributes

fortunately this is the current area of my site I am working on and I too will be listing attributes and add to cart directly in products-list for certain categories, and I already know what basically needs to be done just haven't done it yet -- so I'll have an answer in a few days maybe?

Link to comment
Share on other sites

*I just read the post after writing this and it seems we've got confused*
*the initial poster wanted features in the back office (Catalog tab -> Features -> Click on a feature to see a list)*

*The following solution is for displaying features in the front end, eg in a search or category view*

:-)

I just managed to do this today;
You have to edit category.php and product-list.tpl as follows;

category.php
(Note this should be in your root directory for the shop, not /classes/Category.php )

around line 60 you need to make it look lke this;

           include(dirname(__FILE__).'/pagination.php');
           $smarty->assign('nb_products', $nbProducts);
           $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);
       }
               //This adds the features for each product to the $products variable that gets used by the template
       foreach($cat_products as $key => $product) {
           $cat_products[$key]['features'] = Product::getFrontFeaturesStatic(intval($cookie->id_lang), intval($product['id_product']));
       }
               // end edited code
       $smarty->assign(array(
           'products' => (isset($cat_products) AND $cat_products) ? $cat_products : NULL,
           'id_category' => intval($category->id),
           'id_category_parent' => intval($category->id_parent)



with that done...

/themes/your_theme/product-list.tpl

>
{if $product.new == 1}{l s='new'}{/if}{$product.name|truncate:45:'...'|escape:'htmlall':'UTF-8'}

{$product.description_short|strip_tags:'UTF-8'|truncate:360:'...'}

                               //this is where we add in a loop to print the features out
                   {if $product.features}
                   <!-- product's features -->
</pre>
<ul>
                   {foreach from=$product.features item=feature}

                                  //you can filter by feature ids, feature name or feature value
                       {if $feature.id_feature == 41 || $feature.name == 'Colour - ' || $feature.value == 'Red'}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8'}
                       {/if}

                   {/foreach}
</ul>
<br>                   {/if}<br>                                // end edited code<br><br><br




Hope that helps

Xavier

Link to comment
Share on other sites

  • 2 months later...

It's weird but it won't show any errors.
I think the problem is in the code pasted on the product-list.tpl because that's the page section that is not showing up. Html in the web browser disappears in that part.
thank you!

Link to comment
Share on other sites

:( Now the whole page works but it won't show any feature. Is there a way to call features directly with var $features ? like in $products ? Just wondering.

Thank you Xav for your help, I will keep searching for a solution and will post it if I find out something.

S.
Link to comment
Share on other sites

Ok sorry I wasn't very clear.

When I said take out the {if} tags I meant just them and not the code in between so it should look something like this

>
{if $product.features}
                   <!-- product's features -->
</pre>
<ul>
                   {foreach from=$product.features item=feature}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8}
                   {/foreach}
</ul>
<br>{/if



If that still doesn't work, check that the info is getting through to the tpl file, try putting the following and it should print out all the features in the array

{$product.features|print_r}

Link to comment
Share on other sites

Hello again Xav, I'd tried before what you've said, it was clear from the beginning but it's not working :S

I think the problem is the "foreach" part of the modification.
Here's my original code:

{if isset($products) AND $products}
 {assign var='liHeight' value=360}
 {assign var='nbItemsPerLine' value=4}
 {assign var='nbLi' value=$products|@count}
 {assign var='nbLines' value=$nbLi/$nbItemsPerLine|ceil}
 {assign var='ulHeight' value=$nbLines*$liHeight}
   <!-- Products list -->


   {foreach from=$products item=product name=products}
   {assign var='productLink' value=$link->getProductLink($product.id_product, $product.link_rewrite, $product.category)}



{$product.name|escape:'htmlall':'UTF-8'}

Ver más >



   {/foreach}


   <!-- /Products list -->
{/if}



Here's what I've tried:

{if isset($products) AND $products}
 {assign var='liHeight' value=360}
 {assign var='nbItemsPerLine' value=4}
 {assign var='nbLi' value=$products|@count}
 {assign var='nbLines' value=$nbLi/$nbItemsPerLine|ceil}
 {assign var='ulHeight' value=$nbLines*$liHeight}
   <!-- Products list -->


   {foreach from=$products item=product name=products}
   {assign var='productLink' value=$link->getProductLink($product.id_product, $product.link_rewrite, $product.category)}



{$product.name|escape:'htmlall':'UTF-8'}

                       //YOUR CODE BEGINNING

               {if $product.features}
                   <!-- product's features -->

                   {foreach from=$product.features item=feature}

                                  //you can filter by feature ids, feature name or feature value
                       {if $feature.id_feature == 41 || $feature.name == 'Colour - ' || $feature.value == 'Red'}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8}
                       {/if}

                   {/foreach}

                   {/if}
                           //END OF YOUR CODE


Ver más >



   {/foreach}


   <!-- /Products list -->
{/if}



then I stripped out all the options you gave me... Isn't anything to do with the nested "foreach" ? humm i'm not that skilled programming to know it.

I keep trying! :D thank you again!!

S.

Link to comment
Share on other sites

Heyyyy! I've found over google this code (the attached one) that came directly form this site... but it's just a .txt and do not say in which treat is on.

Now it's working, the only thing left to do for me is getting it to show only one desired feature :D

let me know what you think

install.txt

Link to comment
Share on other sites

Hey,

Yeah that looks pretty good - I'd already started typing the following reply before you posted that so I'll go ahead and finish it. Hopefully it'll help other people understand whats going on. Just for you Susi, here's what you need to know.

//this is a comment


Those two forward slashes indicate a comment that's written in the code. If people paste stuff up here it's good to read it even if you don't understand everything that's going on because you'll notice that I've put comments in my examples explaining what's going on.
The other thing is that you need to define what you want in the category.php file, then assign it to a Smarty variable, then do stuff with it in the .tpl file. If you don't do the first bit, nothing is gonna show up! I've pasted new code below and an explanation below that.
-------------------------

try pasting this instead

>
//YOUR CODE BEGINNING

               {if $product.features}
                   <!-- product's features -->
</pre>
<ul>
                   {foreach from=$product.features item=feature}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8}
                   {/foreach}
</ul>
<br>               {/if}<br><br>//END OF YOUR COD



I understand that you might not be a programmer with 20 years experience or whatever, so I'll try and explain whats going on with this code for everyone because it seems to be giving a few people problems.

*Prestashop in general*
Prestashop is split into three main areas if you're making alterations - the logic, the actions and the views.
The logic is the .php files in your root directory
The actions are the php functions, housed in your /classes directory
The views are the .tpl files which are in the /themes/... directory

You don't need to worry about the classes for now, so ignore those for a second. So right down to basics you have logic in the category.php file, and the view which is in the .tpl file.
This is using something called the Smarty templating system (.tpl ... template... )which is a tool that allows PHP developers to split the logic and the layout in this way. Read up on it if you're not sure or want to find out more.

So here's the important bit- *the .tpl file is not where the action happens* :)
It doesn't really do a lot by itself. You'll notice that you've got all these variables in there but they don't get there by magic. They have to be assigned in the .php file. So let's say for example we want to echo the word 'foo' from categories.php, and get it to show up in the category.tpl template and therefore the final page you need to do the following.

in categories.php - this is standard php

...
$variableToPrint = "foo";

//we assign it to smarty
$smarty->assign('variableForTemplate', variableToPrint);

//we tell smarty to turn this php into a webpage using the tpl file
$smarty->display(_PS_THEME_DIR_.'category.tpl');



so then this gets sent to the tpl file *as a variable*

in the category.tpl file


{$variable for template}



this will make "foo" a

header

That's the basics of it.

So what you're trying to do with this features task is;
1.) generate an array with the features in the category.php file

//This adds the features for each product to the $products variable that gets used by the template
       foreach($cat_products as $key => $product) {
           $cat_products[$key]['features'] = Product::getFrontFeaturesStatic(intval($cookie->id_lang), intval($product['id_product']));
       }


2.) assign it to a smarty variable

$smarty->assign(array(
           'products' => (isset($cat_products) AND $cat_products) ? $cat_products : NULL,
           'id_category' => intval($category->id),
           'id_category_parent' => intval($category->id_parent)


3.) then do stuff with it in the tpl file

{foreach from=$product.features item=feature}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8}
{/foreach}



sorry for the exhaustive reply, hope it helps

Xav

Link to comment
Share on other sites

Thank you so much for the explanation, that made a lot of things very clear now. I did the code modification inside category.php from the very beginning and leave it there all the time, that's how I found out that the problem was in the modification to the .tpl file.

I've played with variables before but smarty is one code I cannot understand that much by now :P

I'm working on a book catalog (http://www.editorialrm.com/2010) and I needed the book authors to show up in the category view, now it is working. I'm goning to try your code because it is easier to understand compared to the one I've found.

For now I made a mixture of both so it shows only one desired feature using that "if $ feature.name == ..." you suggested from the begining, and worked like a charm :) Now I'm gonna find out how to add this feature I need into the new products block in the right bar :P

Again, thank you so much for helping me!!!

S.

Link to comment
Share on other sites

That's great! Glad I could help.

If Gurpreet, the original poster is reading this, could they change the title of the thread and put [sOLVED] at the beginning.

Smarty isn't too hard to learn btw, as the smarty website has pretty good documentation. Once you've figured out how to assign variables in the php file and then pass those to the template, you're off to a good start.

If you want to check out how I've done it on my site, have a look at http://www.forsyths.co.uk/sheet-music - if this code has helped you, a link is always appreciated ;)

Thanks
Xav

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Thank you, works perfectly!

xavriley, I have one question though: I have a feature which was created specifically for displaying in product list only ({if $feature.id_feature == 23}). How can I prevent this feature from appearing on product page (and most important, from opening a new 'data sheet' tab)?

Link to comment
Share on other sites

I did it the way that IF there is a feature 23 it will have different class:

                     {if $feature.id_feature == 23} class="23-{$lang_iso}"{/if}
          {/foreach}>



How to combine the above statement with

{if $product.new == 1}

so if there is a feature id 23, it will be one class, and if the product is new, then another class?

Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...
  • 1 month later...

Hi all,

nice piece of code and explanation xavriley, works perfectly!

now, the next step in my looong way to put my shop online is to show the attributes. I guess the modification are lot more. Did someone achieve to make it? maybe we can try to make it together...Bharadwaj? Rod? any other?

regards,
Jose

Link to comment
Share on other sites

  • 4 weeks later...
  • 2 weeks later...


/themes/your_theme/product-list.tpl

>
{if $product.new == 1}{l s='new'}{/if}{$product.name|truncate:45:'...'|escape:'htmlall':'UTF-8'}

{$product.description_short|strip_tags:'UTF-8'|truncate:360:'...'}

                               //this is where we add in a loop to print the features out
                   {if $product.features}
                   <!-- product's features -->
</pre>
<ul>
                   {foreach from=$product.features item=feature}

                                  //you can filter by feature ids, feature name or feature value
                       {if $feature.id_feature == 41 || $feature.name == 'Colour - ' || $feature.value == 'Red'}
{$feature.name|escape:'htmlall':'UTF-8'} {$feature.value|escape:'htmlall':'UTF-8'}
                       {/if}

                   {/foreach}
</ul>
<br>                   {/if}<br>                                // end edited code<br><br><br




Hope that helps

Xavier



Thanks for the help.

I used elements of that code to display a specific product feature on my Featured Products block on the homepage.
Link to comment
Share on other sites

  • 2 months later...

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