How-To: Add new Properties to your Products
hi folks,
this is a little howto add new properties to general products
(like guarantee time, color, oem-number, compatible models, whatsoever)- think
decide which new properties you want to add to your products
(in this example we take guarantee-time (in months) and color)
. - db manipulation
open the table "products" in your zencart-database and insert new rows at the end of the table called "products_guarantee" and "products_color"
ALTER TABLE `zencart_products` ADD `products_guarantee` INT NOT NULL , ADD `products_color` VARCHAR( 32 ) NOT NULL;
.
- edit 'collect_info.php' (in /admin/includes/modules/product/)
- at the very beginning there is the variable $parameters set. add your row-names to the end:
'products_guarantee' => '0', 'products_color' => '' );
. - just below there is the db-query set [$product = $db->Execute("...]
add your rows with a 'p.' in front (before the "from ..." part)
select ......., p.products_guarantee, p.products_color from ....
. - now you can add the input fields in the product-mask (lets say somewhere around line 450 or so):
<tr>
<td class="main">Guarantee Time (in months)</td>
<td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '15') . ' ' . zen_draw_input_field('products_guarantee', $pInfo->products_guarantee, zen_set_field_length(TABLE_PRODUCTS, 'products_guarantee')); ?></td>
</tr><tr>
<td class="main">Color</td>
<td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '15') . ' ' . zen_draw_input_field('products_color', $pInfo->products_color, zen_set_field_length(TABLE_PRODUCTS, 'products_color')); ?></td>
</tr>
.
- edit 'preview_info.php' (in /admin/includes/modules/product/)
somewhere around line 10 is the variable $product set. like in step 3.2 add the new rows with a 'p.' in front just before the 'from'
select ......., p.products_guarantee, p.products_color from ....
. - finally, edit 'update_product.php' (in /admin/includes/modules/)
around line 20 is the $sql_data_array set. add to the last lines before ');' the new rownames.
$sql_data_array = .......... 'products_guarantee' => zen_db_prepare_input($_POST['products_guarantee']), 'products_color' => zen_db_prepare_input($_POST['products_color']) );
. - Setup All Done!
.
.:D
.
.Of course, now we want to display our cool new properties .
.
- edit 'main_template_vars.php' (in /includes/modules/pages/product_info/)
around line 46 you find the variable $sql set. like in step 3.2 add the new rows with a 'p.' in front just before the 'from'
select ......., p.products_guarantee, p.products_color from ....
. - last step: display the new infos
edit 'tpl_product_info_display.php' (in /includes/templates/template_default/templates/)
now, wherever you want, you can add to the html:
<?php echo $product_info->fields['products_guarantee']; ?> and <?php echo $product_info->fields['products_color']; ?>
enjoy!
ps: now i'm feeling really ############ up : P
:blink::blink::blink:
written by chris at linuxuser.at
Re: How-To: Add new Properties to your Products
Personally, I prefer to put new product properties into a separate table rather than the products table - you never know when the core code is going to change and perhaps encounter problems if you've added extra fields to a table (ever have a mysql error telling you the column count doesn't match?). It's a little more work to achieve the same result, but could save lots of time bug-hunting in the future.
Re: How-To: Add new Properties to your Products
That's a really good point. I added a new field as described above but may go back at some point (in my copious free time - ha!) and change it to a separate table.
Re: How-To: Add new Properties to your Products
I would like to make the output conditional upon a value being present in the DB. Could you expand on your PHP a little bit. I am having a little trouble with the logic.
Guarantee: <?php echo $product_info->fields['products_guarantee']; ?>
Color: <?php echo $product_info->fields['products_color']; ?>
I would only like this to display if a value is present. I can't seem to come up with a conditional statement that works properly.
Any help is greatly appreciated.
Thanks.
Re: How-To: Add new Properties to your Products
Guarantee:
<?php if (!empty($product_info->fields['products_guarantee'])) echo $product_info->fields['products_guarantee']; ?>
Re: How-To: Add new Properties to your Products
Crazy Chris,
Thanks for the help.
roblaw
Re: How-To: Add new Properties to your Products
Quote:
Originally Posted by
crazy_chris
Guarantee:
<?php if (!empty($product_info->fields['products_guarantee'])) echo $product_info->fields['products_guarantee']; ?>
Very useful! Thanks guys
Re: How-To: Add new Properties to your Products
Quote:
Originally Posted by
bunyip
Personally, I prefer to put new product properties into a separate table rather than the products table - you never know when the core code is going to change and perhaps encounter problems if you've added extra fields to a table (ever have a mysql error telling you the column count doesn't match?). It's a little more work to achieve the same result, but could save lots of time bug-hunting in the future.
It makes sense to do the separate table so what needs to be done to integrate it? I understand how to make a new table in MySQL without any problems but I'm fairly new in working with PHP code. So do you have the changes available to share so some of us can set it up this way?
Re: How-To: Add new Properties to your Products
Quote:
Originally Posted by
bunyip
Personally, I prefer to put new product properties into a separate table rather than the products table - you never know when the core code is going to change and perhaps encounter problems if you've added extra fields to a table (ever have a mysql error telling you the column count doesn't match?). It's a little more work to achieve the same result, but could save lots of time bug-hunting in the future.
Have you (or has anyone else) done this? If so, would they be willing to share the code?
Re: How-To: Add new Properties to your Products
Wow, I wish I'd seen this sooner. I've spent so much time reading 23 pages of piece-meal instructions on the Additional Products Fields contribution, only to find out that TheOracle is no longer around to help me make it work. His contribution still involves editing so many files that this seems just as good.
I would love to see step-by-step instructions like those from crazy_chris but using the new table, like the others have suggested.
Alternatively, is there a way to manually asign product id numbers? My main problem is that I want to show both the manufacturer's model number & my sku number. The dev team seems to suggest using the 'model' field for your sku, but I need both. Using my sku as the id would be ideal.
Thanks in advance for any suggestions!