I needed to create additional fields for products in Zen Cart – namely 2 extra description fields for a “Soap” product being sold in the store
Now in the past I have hacked Zen Cart – like using the products_url field as a fake extra description field!
But thought I would investigate the “Proper” way of doing it.
Product Types are the way to go – you know in the admin screen when you click on “New Product” there is a drop down of different product types.
Well it looks like the official way to go is to create a new one of these.
So following the instructions in the Zen Cart guide here:
http://www.zen-cart.com/wiki/index.php/Product_Types
I wung my way through it:
1) First (via phpMyAdmin) I added a new product type to the product types table called “Product – Soap” with a “handler of “product_soap” (stick to this convention as you should be able to see which bits you need to duplicate from existing handlers – like product or product_music)
2) Then I copied the product.php file (in the root of the admin folder) and renamed it product_soap.php– this is the handler file
3) Then I copied the whole of the admin\include\modules\product folder and renamed it to admin\include\modules\product_soap
4) I also copied from the admin\include\modules “root” folder the files “update_product.php“, “preview_info.php” and “copy_to_confirm.php” to my new handler folder as these files would need slight tweaking to add the new fields I was adding
5) Mustn’t forget the language file else none of the labels in the handler file will mean anything, so copy includes\languages\english\product.php to includes\languages\english\product_soap.php
6) Dont forget to add the new product type to includes/filenames.php (just spent 2 hours trying to figure out why the CEON URI rewriter wasnt working for my new product types – doh!)
We’ve now got a back end framework that we can work with.
I want to add two new description fields so that the client can add descriptive information in 3 places (I have previously tried to “train” them to use HTML div’s and separators to frustratingly/horrific results!)
1) Edit the product description table in phpMyAdmin and add two new field products_description2 and products_description3 using same settings as the existing products_description field
2) In the collect_info.php in the new handler folder you will see various lines relating to products_description field.
I simply did a search for “products_description” and added extra bits to include the two other field
3) Do the same thing in the files preview_info.php & copy_to_confirm.php – do both a search for products_description and copy the code for that part & scan through it by eye.
3) The tricky bit came when I was trying to display the products_description2 and 3 in the the page. Zent Cart uses a function called “zen_get_products_description()” – which I obviously didn’t want to start hacking for my meagre purposes so I added a file called “product_soap_functions.php” in the folder “admin\includes\functions\extra_functions” containing the following code
function zen_get_products_description2($product_id, $language = ”) {
global $db;
if (empty($language)) $language = $_SESSION[‘languages_id’];
$product_query = “select products_description2
from ” . TABLE_PRODUCTS_DESCRIPTION . “
where products_id = ‘” . (int)$product_id . “‘
and language_id = ‘” . (int)$language . “‘”;
$product = $db->Execute($product_query);
return $product->fields[‘products_description2’];
}
function zen_get_products_description3($product_id, $language = ”) {
global $db;
if (empty($language)) $language = $_SESSION[‘languages_id’];
$product_query = “select products_description3
from ” . TABLE_PRODUCTS_DESCRIPTION . “
where products_id = ‘” . (int)$product_id . “‘
and language_id = ‘” . (int)$language . “‘”;
$product = $db->Execute($product_query);
return $product->fields[‘products_description3’];
}
And lo – it was imported in automatically once I had used the two new functions zen_get_products_description2() and zen_get_products_description3() in the collect_info.php page
To check this I created a product in the backend and checked it a) showed the new description fields and b) saved them and c) allowed me to edit them correctly
To view the page I needed to create
1) A copy of tpl_product_display_info.php called tpl_product_soap_display_info.php – show the new fields somewhere on this page
2) Copy of the folder “includes/modules/pages/product_info” and call it called includes/modules/pages/product_soap_info
3) Change the main_template_vars.php in the file to pull in the new description fields & importantly- change line that says
$tpl_page_body = ‘/tpl_product_soap_info_display.php’; (line 37)
(why this isn’t automatic I dunno!)
2) A copy of /includes/languages/english/product_info.php called/includes/languages/english/product_soap_info.php and change any display fields in here for your new product type.
Save and view the newly created soap product and enjoy the display of your two new descript ion fields…
Next step now will be to figure out how we can get Easy Populate to look at these fields and import them. Obviously I’ll post here when I find out…
UPDATE: See this post for how to hide all other product types: http://blog.kineticpulse.co.uk/2011/05/custom-product-type-in-zen-cart-how-to.html