Zen Cart custom software development, Zen Cart modules, Zen Cart Expert eCommerce with Zen Cart!

Adding a New Field to a Zen Cart Table

Donate: This is free software. Show your appreciation by supporting my efforts. Donate

Relevance: Zen Cart™ 1.5.x.

Adding a Radio Button field to the Zen Cart Products Table

For this example, we'll add the field "is_kit" to the Products table, which is needed by the mod Kitbuilder.
This field is a radio button, which is either Yes or No.

  1. Add the new field to the database. Go to Admin > Tools > Install SQL Patches, and in the box, enter
    ALTER TABLE products ADD is_kit tinyint(1) default '0' NOT NULL;
    
    (Note that in the actual Kitbuilder mod, this is done in the provided .sql file. This step is shown for completeness only.)
  2. Edit the file admin/includes/languages/english/product.php
    All versions: add the following line:
    define('TEXT_IS_KIT', 'Is Product Kit?');
    

  3. Edit the file admin/includes/modules/update_product.php
    All versions: below the line
                                'products_model' => zen_db_prepare_input($_POST['products_model']),^M
    
    add
                                'is_kit' => zen_db_prepare_input((int)$_POST['is_kit']),
    

  4. Edit the file admin/includes/modules/product/collect_info.php
    1.5.7 and later: skip this step.
    1.5.6 and earlier: Change the select statement that uses
    p.product_is_free, p.product_is_call,
    
    to
    p.is_kit, p.product_is_free, p.product_is_call,
    
    There are additional changes to this file but they depend on your Zen Cart version.
  5. Continue editing admin/includes/modules/product/collect_info.php
    In Zen Cart 1.5.7, look for
      <div class="form-group">
          <p class="col-sm-3 control-label"><?php echo TEXT_PRODUCT_IS_FREE; ?></p>
    
    Above this, add
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_IS_KIT, 'is_kit', 'class="col-sm-3 control-label"'); ?>
        <div class="col-sm-9 col-md-6">
          <label class="radio-inline"><?php echo zen_draw_radio_field('is_kit', '1', ($pInfo->is_kit == 1)) . TEXT_YES; ?></label>
          <label class="radio-inline"><?php echo zen_draw_radio_field('is_kit', '0', ($pInfo->is_kit == 0)) . TEXT_NO; ?></label>
        </div>
      </div>
    

    In Zen Cart 1.5.6, look for
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_PRODUCT_IS_FREE,  ... 
    
    Above this, add
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_IS_KIT, 'is_kit', 'class="col-sm-3 control-label"'); ?>
        <div class="col-sm-9 col-md-6">
          <label class="radio-inline"><?php echo zen_draw_radio_field('is_kit', '1', ($pInfo->is_kit == 1)) . TEXT_YES; ?></label>
          <label class="radio-inline"><?php echo zen_draw_radio_field('is_kit', '0', ($pInfo->is_kit == 0)) . TEXT_NO; ?></label>
        </div>
      </div>
    
    In Zen Cart 1.5.5 and below,
    Above the comment
    // Product is Free
    
    add
    // is_kit
        if (!isset($pInfo->is_kit)) $pInfo->is_kit = '0';
        switch ($pInfo->is_kit) {
          case '0': $in_is_kit = false; $out_is_kit = true; break;
          case '1': $in_is_kit = true; $out_is_kit = false; break;
          default: $in_is_kit = false; $out_is_kit = true;
        }
    
    Then above
              <tr>
                <td class="main"><?php echo TEXT_PRODUCT_IS_FREE; ?></td>
    
    
    add
    
              <tr>
                <td class="main"><?php echo TEXT_IS_KIT; ?></td>
                <td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '15') . ' ' . zen_draw_radio_field('is_kit', '1', ($in_is_kit==1)) . ' ' . TEXT_YES . '  ' . zen_draw_radio_field('is_kit', '0', ($in_is_kit==0)) . ' ' . TEXT_NO; ?>
                </td>
              </tr>
    

Note: These instructions are for an unmodified cart using the Product General product type. If you have used other product types or modified your cart, you will want to apply these modifications to the other copies of collect_info.php and update_product.php.

Adding a Text/Integer Field to the Zen Cart Products Table

The process for adding a text field (a product SKU, for example) is almost the same as what is shown above.
  1. Add the new field to the database. Go to Admin > Tools > Install SQL Patches, and in the box, enter
    ALTER TABLE products ADD sku varchar(64) default 'SKU-' NOT NULL;
    
  2. Edit the file admin/includes/languages/english/product.php
    Add the following line:
    define('TEXT_SKU', 'SKU: ');
    

  3. Edit the file admin/includes/modules/update_product.php
    Below the line
                                'products_model' => zen_db_prepare_input($_POST['products_model']),^M
    
    add
                                'sku' => zen_db_prepare_input($_POST['sku']),
    
    Note that in newer MySQL versions that use STRICT mode, you have to cast optional integer values to ensure you're not trying to put ' ' in an integer field.
                                'some_intval' => (int)zen_db_prepare_input($_POST['some_intval']),
    

  4. Edit the file admin/includes/modules/product/collect_info.php
    1.5.7 and later: skip this step.
    1.5.6 and earlier: Change the select statement that uses
    p.product_is_free, p.product_is_call,
    
    to
    p.sku, p.product_is_free, p.product_is_call,
    
    There are additional changes to this file but they depend on your Zen Cart version.
  5. Continue editing admin/includes/modules/product/collect_info.php
    In Zen Cart 1.5.7, look for
      <div class="form-group">
          <p class="col-sm-3 control-label"><?php echo TEXT_PRODUCT_IS_FREE; ?></p>
    
    Above this, add
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_SKU, 'sku', 'class="col-sm-3 control-label"'); ?>
        <div class="col-sm-9 col-md-6">
            <?php echo zen_draw_input_field('sku', htmlspecialchars(stripslashes($pInfo->sku), ENT_COMPAT, CHARSET, TRUE), zen_set_field_length(TABLE_PRODUCTS, 'sku') . ' class="form-control"'); ?>
        </div>
      </div>
    

    In Zen Cart 1.5.6,
    Look for
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_PRODUCT_IS_FREE,  ... 
    
    Above this, add
      <div class="form-group">
          <?php echo zen_draw_label(TEXT_SKU, 'sku', 'class="col-sm-3 control-label"'); ?>
        <div class="col-sm-9 col-md-6">
            <?php echo zen_draw_input_field('sku', htmlspecialchars(stripslashes($pInfo->sku), ENT_COMPAT, CHARSET, TRUE), zen_set_field_length(TABLE_PRODUCTS, 'sku') . ' class="form-control"'); ?>
        </div>
      </div>
    
    In Zen Cart 1.5.5 and below
    Above
              <tr>
                <td class="main"><?php echo TEXT_PRODUCT_IS_FREE; ?></td>
    
    
    add
    
              <tr>
                <td class="main"><?php echo TEXT_SKU; ?></td>
                <td class="main"><?php echo zen_draw_separator('pixel_trans.gif', '24', '15') . ' ' . zen_draw_input_field('sku', htmlspecialchars(stripslashes($pInfo->sku), ENT_COMPAT, CHARSET, TRUE), zen_set_field_length(TABLE_PRODUCTS, 'sku'));?> 
                </td>
              </tr>
    

Note: For modified carts or using other product types, please see instructions above.

Note: If you prefer to use the Zen Cart naming scheme, prefixing product fields with the string "products_", just be sure you do it consistently. So in the example above, you would change:
  • TEXT_SKU to TEXT_PRODUCTS_SKU
  • $pInfo->sku to $pInfo->products_sku
  • 'sku' to 'products_sku'
  • p.sku to p.products_sku
  • ALTER TABLE products ADD sku to ALTER TABLE products ADD products_sku


Adding Checkbox field to the Customers Table

An example of how to add a checkbox to the Customers table is provided in the Optional Payment Method mod.

Too many steps? I can do this for you if you'd like! Just contact me with your project specifications and I'll build you a quote.