Zen Cart custom software development, Zen Cart modules, Zen Cart Expert eCommerce Websites 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 Radio Button field to the 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. Modify the file admin/includes/languages/english/product.php and add the following line:
    define('TEXT_IS_KIT', 'Is Product Kit?');
    
  3. Modify the file admin/includes/modules/update_product.php and 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. Modify the file admin/includes/modules/product/collect_info.php and 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. In Zen Cart 1.5.5 and below, continue editing admin/includes/modules/product/collect_info.php.
    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>
    
  6. In Zen Cart 1.5.6 and above, continue editing admin/includes/modules/product/collect_info.php.
    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>
    

Adding Text Field to the 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. Modify the file admin/includes/languages/english/product.php and add the following line:
    define('TEXT_SKU', 'SKU: ');
    
  3. Modify the file admin/includes/modules/update_product.php and below the line
                                'products_model' => zen_db_prepare_input($_POST['products_model']),^M
    
    add
                                'sku' => zen_db_prepare_input((int)$_POST['sku']),
    
  4. Modify the file admin/includes/modules/product/collect_info.php and 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. In Zen Cart 1.5.5 and below, continue editing admin/includes/modules/product/collect_info.php.
    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>
    
  6. In Zen Cart 1.5.6 and above, continue editing admin/includes/modules/product/collect_info.php.
    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>
    

Adding Radio Button field to the Customers Table

An example of how to add a radio button 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.