Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

How to add extra field values to the product list page

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 02-26-2016, 03:31 PM
 
rob@equivox.com rob@equivox.com is offline
    
Join Date: Jan 2016
Posts: 1
 

Default How to add extra field values to the product list page

Thought I would share this since the older answers do not really work...in XCart 4.7, if you want to have extra fields on the product listing page, you must add the fields to the product loop in products.php...i struggled with this for a few days, but with xcart support's help, i was able to craft this very simple code for my extra fields to show on the product listing page...

In products.php

just before the line

$smarty->assign('cat_products', isset($products) ? $products : array());

add this code and be sure to change the values to match your extra fields...

foreach($products as $c => $v) {
$products[$c]['calories'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid = '2'");
$products[$c]['protein'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid = '3'");
$products[$c]['fat'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid = '5'");
$products[$c]['carbs'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid = '4'");
}
$smarty->assign('calories', $calories);
$smarty->assign('protein', $protein);
$smarty->assign('fat', $fat);
$smarty->assign('carbs', $carbs);

pay attention to the 'fieldid' in the script...it is at the end of each line and you must figure out which fieldid in the database you want to have the value from...look in the table 'xcart_extra_field_values' and look at the 'fieldid' column and match that to the extra field you are trying to show[/b]

in my example, i have some food stuff...

Calories, Fat, Protein, Carbs...they are all extra fields and i went into the aforementioned tabled and got their 'fieldid's and then i put them into this script...

THEN in products_t.tpl
add these smarty tags wherever you want to show the extra field values...be sure to add these tags inside the {foreach}{/foreach} loop

{$product.calories}
{$product.protein}
{$product.fat}
{$product.carbs}
__________________
rs
Reply With Quote
  #2  
Old 03-28-2016, 03:58 PM
  DrQuietus's Avatar 
DrQuietus DrQuietus is offline
 

Advanced Member
  
Join Date: Dec 2005
Posts: 81
 

Default Re: How to add extra field values to the product list page

I am getting a sql error, can you tell why?

Quote:
SQL query : SELECT value FROM WHERE productid='24' AND fieldid='16' LIMIT 1
Error code : 1064
Description : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE productid='24' AND fieldid='16' LIMIT 1' at line 1

Quote:
$products[$c]['WHEEL_MATERIAL'] = func_query_first_cell("SELECT value FROM $sql_tbl[xcart_extra_field_values] WHERE productid='$v[productid]' AND fieldid='16'");
__________________
Dave Jones
dave@industrialwebworks.net
Mostly 4.7.2 - 4.7.5
Reply With Quote
  #3  
Old 03-28-2016, 04:20 PM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,534
 

Default Re: How to add extra field values to the product list page

Change:

FROM $sql_tbl[xcart_extra_field_values]

to:

FROM $sql_tbl[extra_field_values]
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote
  #4  
Old 03-28-2016, 04:22 PM
  DrQuietus's Avatar 
DrQuietus DrQuietus is offline
 

Advanced Member
  
Join Date: Dec 2005
Posts: 81
 

Default Re: How to add extra field values to the product list page

I've tried all of the below, same syntax error message, same line. Verified it is fieldid 16.

Code:
$products[$c]['Wheel Material'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid='16'");

Code:
$products[$c]['Wheel Material'] = func_query_first_cell("SELECT value FROM $sql_tbl[xcart_extra_field_values] WHERE productid='$v[productid]' AND fieldid='16'");

Code:
$products[$c]['WHEEL_MATERIAL'] = func_query_first_cell("SELECT value FROM $sql_tbl[extra_field_values] WHERE productid='$v[productid]' AND fieldid='16'");

Code:
$products[$c]['WHEEL_MATERIAL'] = func_query_first_cell("SELECT value FROM $sql_tbl[xcart_extra_field_values] WHERE productid='$v[productid]' AND fieldid='16'");
__________________
Dave Jones
dave@industrialwebworks.net
Mostly 4.7.2 - 4.7.5
Reply With Quote
  #5  
Old 03-28-2016, 04:36 PM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,534
 

Default Re: How to add extra field values to the product list page

$sql_tbl must be present (did you check?). Try adding this before the foreach:

PHP Code:
global $sql_tbl
However, I have two concerns with this customization: database queries should not be in a loop if you can help it, especially one like this that could have a lot of products; and Extra Fields returned to product listings in 4.1, so the data is already there (https://forum.x-cart.com/showthread.php?p=118321#post118321 [watch for typos]).
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote
  #6  
Old 03-28-2016, 04:36 PM
 
equivox1 equivox1 is offline
 

Newbie
  
Join Date: Jan 2012
Posts: 2
 

Default Re: How to add extra field values to the product list page

can you post all the relevant code you created for products.php and products_t.tpl?
__________________
Equivox
Reply With Quote
  #7  
Old 03-28-2016, 04:57 PM
  DrQuietus's Avatar 
DrQuietus DrQuietus is offline
 

Advanced Member
  
Join Date: Dec 2005
Posts: 81
 

Default Re: How to add extra field values to the product list page

If the data is available, can you tell why this is not returning any values:

Code:
<table cellpadding="3" cellspacing="0" class="prodlist-table"> <thead> <tr> <th>Part #</th> {section name=product loop=$products} {if $smarty.section.product.first} {foreach from=$products[product].extra_fields item=extra_field name=field} {if $extra_field.value ne ''} <th>{$extra_field.field}</th> {/if} {/foreach} {/if} {/section} </tr> </thead> <tbody> {foreach from=$products item=product name=products} <script type="text/javascript"> //<![CDATA[ products_data[{$product.productid}] = {ldelim}{rdelim}; //]]> </script> {if $active_modules.Product_Configurator and $is_pconf and $current_product} {assign var="url" value="product.php?productid=`$product.productid`&amp;pconf=`$current_product.productid`&amp;slot=`$slot`"} {else} {assign var="url" value="product.php?productid=`$product.productid`&amp;cat=`$cat`&amp;page=`$navigation_page`"} {if $featured eq 'Y'} {assign var="url" value=$url|cat:"&amp;featured=Y"} {/if} {/if} <tr class="{cycle values="odd,even"} product-name-row highlight product-cell product-cell-buynow"> <td><a href="{$url}" class="product-title">{$product.productcode|escape}</a></td> {foreach from=$product.extra_fields item=extra_field name=field} {if $extra_field.field_value ne ""} <td align="center">{$extra_field.field_value}</td> {/if} {/foreach} </tr> {/foreach} </tbody> </table>
__________________
Dave Jones
dave@industrialwebworks.net
Mostly 4.7.2 - 4.7.5
Reply With Quote
  #8  
Old 03-28-2016, 05:21 PM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,534
 

Default Re: How to add extra field values to the product list page

Print the array to see if the data is there:

<pre>{$products|@print_r}</pre>

Make sure you are using .value and not .field_value (as in one of your loops). You could try removing some of the extra code and experiment with the raw loop. Here is a sample I have used:

Code:
{foreach from=$products item=product name=products} {if $product.extra_fields ne ''} {foreach from=$product.extra_fields item=extra_field} {if $extra_field.service_name eq "ISBN" and $extra_field.value} {assign var="isbn_val" value=$extra_field.value}
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote
  #9  
Old 03-28-2016, 09:40 PM
 
equivox1 equivox1 is offline
 

Newbie
  
Join Date: Jan 2012
Posts: 2
 

Default

try getting the example to code to work EXACTLY as it is presented with only one field first...i would suggest you make your extra field names simpler, they do not have to be capitalized or have underscores, they are for your reference, they do not display anywhere but in admin, so try to make them one word, lowercase...

do not add any other loops outside the main product loop...use my example as is and then go from there once it works...

if you are simply inside the main products loop you do not need to add other loops...the extra fields values will already be available in the main loop because of the code added to products.php...you only need the extra field smarty tag for each field you set up...

{$product.WHEEL_MATERIAL}

just drop that into the template, you do not need extra loops anywhere and you certainly can't use this outside of the main loop

and you did not post your code for products.php, so i cannot see if there is a problem there...just try to follow my example exactly as i have made it...
__________________
Equivox

Last edited by qualiteam : 03-31-2016 at 03:24 AM.
Reply With Quote
  #10  
Old 03-29-2016, 06:55 AM
  DrQuietus's Avatar 
DrQuietus DrQuietus is offline
 

Advanced Member
  
Join Date: Dec 2005
Posts: 81
 

Default Re: How to add extra field values to the product list page

print_r "not allowed by security settings". But looking in webmaster mode/Show variables it appears to now be $cat_products (it works the same as $products too), but there are no extra fields.

this returns only productid:
Code:
{foreach from=$cat_products item=product name=products} {$product.productid}<br> {foreach from=$product.extra_fields item=extra_field} REPEAT SOMETHING {/foreach} {/foreach}

So does this:
Code:
{foreach from=$products item=product name=products} {$product.productid}<br> {foreach from=$product.extra_fields item=extra_field} REPEAT SOMETHING {/foreach} {/foreach}

It appears the data has been removed...again? Maybe they removed it because like you said database queries shouldn't be in a loop?

So are we back to the original question - how to query the data from the extra_field_values table? Or do you think there is something else to try? Does this apply: http://stackoverflow.com/questions/15262082/add-sql-php-array-to-an-already-existing-array

Why do you suppose in rob's solution they did the extra fields individually?

I forgot to mention, thank you for helping out Cherie.
__________________
Dave Jones
dave@industrialwebworks.net
Mostly 4.7.2 - 4.7.5
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 04:57 AM.

   

 
X-Cart forums © 2001-2020