Zen Cart Logo
Forums / Setting Up Categories, Products, Attributes / How To Add New Product Fields?

How To Add New Product Fields?

Views: 83,902

Results 1 to 20 of 159
18 Feb 2009, 12:52 PM
#1
webego avatar

webego

New Zenner

Join Date:
Oct 2008
Posts:
36
Plugin Contributions:
0

How To Add New Product Fields?

After searching through the Zen Cart forums for help on how to add new product fields, I came across a thread started by crazy_chris which shows how to add a new field to the existing products table. Later in that thread zskiman shows how to add fields into a separate table, which is a great idea to future proof your efforts for later upgrades etc. My intention here is to share what I have learned from both these posts and add my own input which I think simplifies the end usage of your new fields.

Any changes to existing code or completely new code are shown in red (what out for , too).

So lets gets started...

  1. Create a new table to hold your additional fields and give it a name i.e. products_extra_stuff and add the products_id field (so we can relate our new table to the existing products table) and whatever fields you wish to add i.e. **products_colour

**
2. Open includes/database_tables.php and add your new table to the list of definitions i.e.define('TABLE_PRODUCTS_EXTRA_STUFF', DB_PREFIX . 'products_extra_stuff');


3. Open admin/includes/modules/product/preview_info.php and change the following lines:

(line 26 for me)
p.products_sort_order, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_EXTRA_STUFF . " pdex
where p.products_id = pd.products_id and p.products_id = pdex.products_id


4. Open admin/includes/modules/product/collect_info.php and change the following lines :

(line 12 for me)
$parameters = array('products_name' => '',
'products_description' => '',
…
'master_categories_id' => '',
'products_colour' => ''

);

(now line 60 for me after the above edit)
p.products_price_sorter, p.master_categories_id, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " .
TABLE_PRODUCTS_EXTRA_STUFF . " pdex

where p.products_id = '" . (int)$_GET['pID'] . "'
and p.products_id = pd.products_id and p.products_id = pdex.products_id


5. In the same file (collect_info.php) insert your form fields to collect your new data along with the rest:

(line 305 for me as I wanted to insert just below product name)
<!-- BOF - Additional field added -->
<tr>

<td class="main"><?php echo 'Colour '; ?></td> <td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '13') . ' ' . zen_draw_input_field('products_colour', $pInfo->products_colour, zen_set_field_length(TABLE_PRODUCTS_EXTRA_STUFF, 'products_colour')); ?></td> </tr> <!-- EOF - Additional field added -->


6. Open admin/includes/modules/update_product.php and add the following lines to process both new products and the editing of existing ones:

(line 153 for me)
////////////// MY ADDED FIELDS ////////////////////
$sql_data_array = array('products_colour' => zen_db_prepare_input($_POST['products_colour']));

if ($action == 'insert_product') {
$insert_sql_data = array('products_id' => $products_id);

      $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

      zen_db_perform(TABLE_PRODUCTS_EXTRA_STUFF, $sql_data_array);

} elseif ($action == 'update_product') {
zen_db_perform(TABLE_PRODUCTS_EXTRA_STUFF, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");
}
/////////////////////////////////////////////


7. In order to make the data from your new field(s) available to add to the product page open inludes/modules/pages/product_info/main_template_vars.php and change the following lines:

(line 55 for me)
p.products_discount_type, p.products_discount_type_from, p.products_sort_order,
p.products_price_sorter, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_EXTRA_STUFF . " pdex
where p.products_status = '1'
and p.products_id = '" . (int)$_GET['products_id'] . "'
and pd.products_id = p.products_id and p.products_id = pdex.products_id

(line 101 for me amongst products name, model and description)
$products_colour = $product_info->fields['products_colour'];


8. Finally, to display the data from your new field(s) open includes/templates/YOUR_TEMPLATE/templates/tpl_product_info_display.php and add something similar to the following:

(place wherever you wish your new field to be displayed)
<!--bof Product Colour -->

<?php if ($products_colour != '') { ?> <div id="productColour" class="productGeneral"><strong>Colour: </strong><?php echo $products_colour; ?></div> <?php } ?> <!--eof Product Colour -->

And that my friends is it...Enjoy!

7 May 2009, 1:55 PM
#2
hj_lubbe avatar

hj_lubbe

New Zenner

Join Date:
Apr 2009
Location:
South Africa
Posts:
6
Plugin Contributions:
0

Re: How To Add New Product Fields?

Hi webego, :D

Thanks for posting this it helped me alot.

I've just got one issue that I just can't seem to get right. Also I don't really know PHP that well.:frusty:

Everything displays fine in Admin and on the Product Info Pages.

I can also update existing products, but when I try to add new products this error pops up:

1062 Duplicate entry '39' for key 'PRIMARY'
in:
[insert into products_extra_stuff (products_serial, products_id) values ('RXA01301', '39')]
If you were entering information, press the BACK button in your browser and re-check the information you had entered to be sure you left no blank fields.

I don't see any duplicates in the table.

Can you please help me? Just let me know if you want to see any code.

TIA
HJ Lubbe

11 May 2009, 6:00 PM
#3
colin0502 avatar

colin0502

New Zenner

Join Date:
May 2009
Posts:
68
Plugin Contributions:
0

Re: How To Add New Product Fields?

hi, i'm new to php and only have little knowledge about database , for the step 6 the update_product.php, you said it's at line 153 for me seems a different line so i would like to ask where these code add to?

i have these default code that similar to what you post:

$languages = zen_get_languages();
for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
  $language_id = $languages[$i]['id'];

  $sql_data_array = array('products_name' => zen_db_prepare_input($_POST['products_name'][$language_id]),
                          'products_description' => zen_db_prepare_input($_POST['products_description'][$language_id]),
                          'products_url' => zen_db_prepare_input($_POST['products_url'][$language_id]));

  if ($action == 'insert_product') {
    $insert_sql_data = array('products_id' => $products_id,
                             'language_id' => $language_id);

    $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

    zen_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array);
  } elseif ($action == 'update_product') {
    zen_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "' and language_id = '" . (int)$language_id . "'");
  }

the code you suggest to add is simliar to the one above but that is inside a for loop as well, so could you tell me where to add these code? or i have to create exactly the same for loop? sorry for the newbie question

12 May 2009, 9:33 PM
#4
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

HJ Lubbe

That error means your product id numbers are off. The cart is trying to insert a product with id 39 which must already be there - or else your mods have created a situation where it's posting twice. You need to access your database and find out what is in the products table and the last id used. If it is the same product as what you were trying to add, then you are adding twice. If not, then you can increase the auto increment by one to make the next id number advance. Auto increment can be viewed on the operations tab for the products table if you are using phpmyadmin.

colin0502

I'm just about to dig in and do this and I'll try to post what you need shortly.

13 May 2009, 12:26 PM
#5
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

One thing that needs to be said first is that instead of editing the database tables file, you need to create a new file in the extra datafiles directory for override purposes. Name product_database_names.php

Follow the protocols in the existing files in that directory.

16 May 2009, 9:06 PM
#6
michaelhl avatar

michaelhl

New Zenner

Join Date:
Oct 2005
Location:
Germany
Posts:
60
Plugin Contributions:
0

Re: How To Add New Product Fields?

Thank you for your explanation. It may be helpfull, but I will wait for 2.0, may be that there is a easier way for it.
At this time I use the book contribution to get new product fields, but it not really work, because deactivated or deleted products not completly away and so no 404 page is generated.
In older times with os-commerce there was a contribution called product_extra_fields. It was very helpful, because you are able to add as many fields you want to your product page. I hope that Zen-Cart 2.0 will allow a easy way to design a own product_type with its fields like the old oscommerce contribution do.

16 May 2009, 11:07 PM
#7
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

I have completed an entirely new mod that incorporates this one - with the extra text field - instead of product code, the 4 possible uploads for pdfs or other files and one for flash as well.

I don't have it ready to upload to contributions yet and it is being tested first as well.

I'll let you know when I put it up.

The instructions are clearer and you will not have to make any file changes if the files have not been already modded in your cart.

18 May 2009, 2:06 AM
#8
zvenson avatar

zvenson

New Zenner

Join Date:
May 2009
Posts:
61
Plugin Contributions:
0

Re: How To Add New Product Fields?

Hi!
Thank you webego for your post! Helped me a lot!
I managed to have dropdown lists for my added product fields and maybe someone wants to do it as well so here`s what i did:

  1. I assume that you already did what webego wrote. This mod is just for the dropdown - the database entry thing should work already

  2. Create a new Table which holds the options of your field - in my case "absorbtionclass" which contains an id field and a field which contains the values.

  3. Open includes/database_tables.php and add your new table to the list of definitions i.e.define('TABLE_ABSO', DB_PREFIX . 'absorbtionclass');

  4. open admin/includes/modules/product/collect_info.php and add

<?php $absorbtion_array = array(array('id' => '', 'text' => TEXT_NONE)); $abso = $db->Execute("select id, absorbtionclass from " . TABLE_ABSO . " order by id"); while (!$abso->EOF) { $absorbtion_array[] = array('id' => $abso->fields['absorbtionclass'], 'text' => $abso->fields['absorbtionclass']); $abso->MoveNext(); } ?>

this creates the array with the options

  1. add (in the same file)
<tr> <td class="main"><?php echo 'class_of_absorbtion '; ?></td>
        <td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '15') . ' ' . zen_draw_pull_down_menu('class_of_absorbtion', $absorbtion_array, $pInfo->class_of_absorbtion); ?></td>
</tr>

that should be it...

hope this helps somebody!

28 May 2009, 6:48 AM
#9
kes99 avatar

kes99

New Zenner

Join Date:
May 2009
Posts:
42
Plugin Contributions:
0

Re: How To Add New Product Fields?

delia:

I have completed an entirely new mod that incorporates this one - with the extra text field - instead of product code, the 4 possible uploads for pdfs or other files and one for flash as well.

I don't have it ready to upload to contributions yet and it is being tested first as well.

I'll let you know when I put it up.

The instructions are clearer and you will not have to make any file changes if the files have not been already modded in your cart.

Sounds like a good idea.....just started using Zen and I'm surprised that nobody had got additional fields in before without such a lot of hassle.....The built in ones are next to useless...

please PM when your mod is ready.....if you'd like me to assist in testing it on let me know......

28 May 2009, 6:51 AM
#10
kes99 avatar

kes99

New Zenner

Join Date:
May 2009
Posts:
42
Plugin Contributions:
0

Re: How To Add New Product Fields?

webego:

After searching through the Zen Cart forums for help on how to add new product fields, I came across a thread started by crazy_chris which shows how to add a new field to the existing products table. Later in that thread zskiman shows how to add fields into a separate table, which is a great idea to future proof your efforts for later upgrades etc. My intention here is to share what I have learned from both these posts and add my own input which I think simplifies the end usage of your new fields.

Any changes to existing code or completely new code are shown in red (what out for , too).

So lets gets started...

  1. Create a new table to hold your additional fields and give it a name i.e. products_extra_stuff and add the products_id field (so we can relate our new table to the existing products table) and whatever fields you wish to add i.e. **products_colour

**

  1. Open includes/database_tables.php and add your new table to the list of definitions i.e.define('TABLE_PRODUCTS_EXTRA_STUFF', DB_PREFIX . 'products_extra_stuff');

  1. Open admin/includes/modules/product/preview_info.php and change the following lines:

(line 26 for me)
p.products_sort_order, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_EXTRA_STUFF . " pdex
where p.products_id = pd.products_id and p.products_id = pdex.products_id

  1. Open admin/includes/modules/product/collect_info.php and change the following lines :

(line 12 for me)
$parameters = array('products_name' => '',
'products_description' => '',
…
'master_categories_id' => '',
'products_colour' => ''

);

(now line 60 for me after the above edit)
p.products_price_sorter, p.master_categories_id, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " .
TABLE_PRODUCTS_EXTRA_STUFF . " pdex

where p.products_id = '" . (int)$_GET['pID'] . "'
and p.products_id = pd.products_id and p.products_id = pdex.products_id

  1. In the same file (collect_info.php) insert your form fields to collect your new data along with the rest:

(line 305 for me as I wanted to insert just below product name)
<!-- BOF - Additional field added -->
<tr>

<td class="main"><?php echo 'Colour '; ?></td> <td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '13') . ' ' . zen_draw_input_field('products_colour', $pInfo->products_colour, zen_set_field_length(TABLE_PRODUCTS_EXTRA_STUFF, 'products_colour')); ?></td> </tr> <!-- EOF - Additional field added -->

  1. Open admin/includes/modules/update_product.php and add the following lines to process both new products and the editing of existing ones:

(line 153 for me)
////////////// MY ADDED FIELDS ////////////////////
$sql_data_array = array('products_colour' => zen_db_prepare_input($_POST['products_colour']));

if ($action == 'insert_product') {
$insert_sql_data = array('products_id' => $products_id);

      $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

      zen_db_perform(TABLE_PRODUCTS_EXTRA_STUFF, $sql_data_array);

} elseif ($action == 'update_product') {
zen_db_perform(TABLE_PRODUCTS_EXTRA_STUFF, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");
}
/////////////////////////////////////////////

  1. In order to make the data from your new field(s) available to add to the product page open inludes/modules/pages/product_info/main_template_vars.php and change the following lines:

(line 55 for me)
p.products_discount_type, p.products_discount_type_from, p.products_sort_order,
p.products_price_sorter, pdex.products_colour
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_EXTRA_STUFF . " pdex
where p.products_status = '1'
and p.products_id = '" . (int)$_GET['products_id'] . "'
and pd.products_id = p.products_id and p.products_id = pdex.products_id

(line 101 for me amongst products name, model and description)
$products_colour = $product_info->fields['products_colour'];

  1. Finally, to display the data from your new field(s) open includes/templates/YOUR_TEMPLATE/templates/tpl_product_info_display.php and add something similar to the following:

(place wherever you wish your new field to be displayed)
<!--bof Product Colour -->

<?php if ($products_colour != '') { ?> <div id="productColour" class="productGeneral"><strong>Colour: </strong><?php echo $products_colour; ?></div> <?php } ?> <!--eof Product Colour -->

And that my friends is it...Enjoy!

So that's it for one extra field but I want more than one....surely not all the above has to be done for each does it?

28 May 2009, 11:39 AM
#11
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

well, I don't know what's happening with our zen leaders so I've uploaded my mod to my website so that everyone can grab it now. Please, if anyone runs into problems, let me know!

The first extra field is now a textarea that can be used for embedding a YouTube video by the way.

Grab the zip file here

Enjoy!

28 May 2009, 2:20 PM
#12
steve_cooper avatar

steve_cooper

New Zenner

Join Date:
May 2009
Posts:
3
Plugin Contributions:
0

Re: How To Add New Product Fields?

Thanks for working this out! I have a newbie question though -

Once I've added additional fields, I imagine that will break EasyPopulate. Have you worked out how to include the additional fields into the import process?

28 May 2009, 2:28 PM
#13
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

sorry, no, if I motivated (paid or bored) I'll look into that!

28 May 2009, 9:41 PM
#14
kes99 avatar

kes99

New Zenner

Join Date:
May 2009
Posts:
42
Plugin Contributions:
0

Re: How To Add New Product Fields?

steve_cooper:

Thanks for working this out! I have a newbie question though -

Once I've added additional fields, I imagine that will break EasyPopulate. Have you worked out how to include the additional fields into the import process?

Good question.....I want to use EasyPopulate as well as the new extra field mod.....I'm working things through on a test Zen-Cart installation till I get everything working right....I'll advise here later after extensive testing.....:smartalec:

28 May 2009, 9:59 PM
#15
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

I know easy populate won't import those extra fields but nothing has been changed in the other tables so easy populate should still work.

I was talking about adding this into easy populate.

28 May 2009, 10:01 PM
#16
delia avatar

delia

Totally Zenned

Join Date:
May 2006
Location:
Gardiner, Maine
Posts:
2,383
Plugin Contributions:
7

Re: How To Add New Product Fields?

One additional note:

The textarea field for the extra field description will show the editor if you have htmlarea turned on. Turn it off to use the field for youtube video embedding.

That change will come later when I figure out how to turn it off. Ran into this before and have never figured it out!

29 May 2009, 4:49 AM
#17
anap avatar

anap

New Zenner

Join Date:
May 2009
Location:
The West Coast
Posts:
25
Plugin Contributions:
0

Re: How To Add New Product Fields?

HJ Lubbe, I believe I know your problem with the duplicate entry error, b/c I ran into the same error. Because the error was on the products_extra_stuff table, it means you've tried to insert multiple entries using the same product_id.

I'm guessing you added more than one custom field. In Step 6, you probably pasted the entire block of code for each extra field. In fact, you only need to do this once and modify the array below, as this gets inserted into the table all at once:

webego:

$sql_data_array = array('products_colour' => zen_db_prepare_input($_POST['products_colour']));

i.e. if you have a 2nd field called "dimensions" you would change the array to:

$sql_data_array = array('products_colour' => zen_db_prepare_input($_POST['products_colour']), 'products_dimensions' => zen_db_prepare_input($_POST['products_dimensions']));

Thanks webego for putting this together. It is very frustrating digging through long forum threads to find step by step instructions as clear as yours. I hope 2.0 has this feature built-in!

31 May 2009, 3:57 AM
#18
kes99 avatar

kes99

New Zenner

Join Date:
May 2009
Posts:
42
Plugin Contributions:
0

Re: How To Add New Product Fields?

anap:

HJ Lubbe, I believe I know your problem with the duplicate entry error, b/c I ran into the same error. Because the error was on the products_extra_stuff table, it means you've tried to insert multiple entries using the same product_id.

I'm guessing you added more than one custom field. In Step 6, you probably pasted the entire block of code for each extra field. In fact, you only need to do this once and modify the array below, as this gets inserted into the table all at once:

i.e. if you have a 2nd field called "dimensions" you would change the array to:

Thanks webego for putting this together. It is very frustrating digging through long forum threads to find step by step instructions as clear as yours. I hope 2.0 has this feature built-in!

I'm pretty sure there are lots of other things with the original post that need amending for multiple custom fields.

1 Jun 2009, 4:09 AM
#19
anap avatar

anap

New Zenner

Join Date:
May 2009
Location:
The West Coast
Posts:
25
Plugin Contributions:
0

Re: How To Add New Product Fields?

KES99:

I'm pretty sure there are lots of other things with the original post that need amending for multiple custom fields.

Other than the array mentioned above, you just repeat the original steps for each field. You are modifying the SQL calls so just make sure the commas are in the right place.

However I'd like to see some sample PHP/HTML for a custom drop-down list field, perhaps fed by a text file. It is probably not difficult but my PHP skills are very limited. I.e. Shirt size XS S M L XL.

3 Jun 2009, 6:11 PM
#20
greengreetings avatar

greengreetings

Zen Follower

Join Date:
May 2009
Location:
Virginia
Posts:
90
Plugin Contributions:
0

Re: How To Add New Product Fields?

I have a question. I followed all of the steps in the first post and when I go to a product in categories I receive this message:

Warning: reset() [function.reset]: Passed variable is not an array or object in /usr/www/users/deleted for privacy/catalog/deleted for privacy/includes/classes/object_info.php on line 29

Warning: Variable passed to each() is not an array or object in /usr/www/users/deleted for privacy/catalog/deleted for privacy/includes/classes/object_info.php on line 30
Product in Category: "Cards"

I don't know what that means nor what I should do. Any suggestions?