Results 1 to 10 of 159

Threaded View

  1. #1
    Join Date
    Oct 2008
    Posts
    36
    Plugin Contributions
    0

    Default 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') . '&nbsp;' .
      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!
    Last edited by webego; 18 Feb 2009 at 01:56 PM.

 

 

Similar Threads

  1. How to add new fields?
    By chandroo007 in forum Upgrading from 1.3.x to 1.3.9
    Replies: 1
    Last Post: 1 Jun 2010, 03:13 PM
  2. How To Add New Fields to Invoice?
    By PudzPud in forum Basic Configuration
    Replies: 7
    Last Post: 15 Nov 2009, 03:50 PM
  3. Replies: 1
    Last Post: 10 Dec 2007, 02:40 PM
  4. How to add labels to new fields
    By kappaluppa in forum Templates, Stylesheets, Page Layout
    Replies: 5
    Last Post: 3 Feb 2007, 07:16 PM
  5. How i can add new data fields to a Product
    By shreesoft in forum Customization from the Admin
    Replies: 4
    Last Post: 2 Oct 2006, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg