Adding X-Cart Custom Product Fields

Added:
Enter Price $
SKU SKU146
Quantity

In this quick v4.4.x - v4.7.x tutorial, we'll show you how to add 3 new custom product fields...

  1. Text Field - Standard text field
  2. Textarea Field - We'll assume you want to put HTML code in this field
  3. Select Dropdown Field - Simple yes or no (or checkbox if you prefer)

For simplicity, we'll name the 3 new fields - cpfield1, cpfield2 and cpfield3 respectively.


First we need to patch the database xcart_products table and add some new language labels...

In Tools > Patch/Upgrade in the SQL query(ies) box paste and apply the following...

ALTER TABLE `xcart_products` ADD `cpfield1` text NULL , ADD `cpfield2` text NULL , ADD `cpfield3` varchar(1) NOT NULL DEFAULT 'N';
REPLACE INTO xcart_languages SET code='en', name='lbl_custom_field_1', value='Custom Field 1', topic='Labels';
REPLACE INTO xcart_languages SET code='en', name='lbl_custom_field_2', value='Custom Field 2', topic='Labels';
REPLACE INTO xcart_languages SET code='en', name='lbl_custom_field_3', value='Custom Field 3', topic='Labels';

Should you need or wish to drop the new fields and data, paste and apply the following SQL patch...

ALTER TABLE `xcart_products` DROP `cpfield1`, DROP `cpfield2`, DROP `cpfield3`;

Next we need to add the 3 entry fields to the product modify pages.

Create a new file called product_custom_fields.tpl and paste the following into it...

<tr>
  <td colspan="3"><hr /></td>
</tr>
{* Custom Product Text Field - cpfield1 *}
<tr> 
  {if $geid ne ''}<td width="15" class="TableSubHead"><input type="checkbox" value="Y" name="fields[cpfield1]" /></td>{/if}
  <td class="FormButton" nowrap="nowrap">{$lng.lbl_custom_field_1}:</td>
  <td class="ProductDetails"><input type="text" name="cpfield1" id="cpfield1" size="20" maxlength="32" value="{$product.cpfield1|escape}" class="InputWidth" /></td>
</tr>
{* Custom Product Textarea Field - cpfield2 *}
<tr> 
  {if $geid ne ''}<td width="15" class="TableSubHead"><input type="checkbox" value="Y" name="fields[cpfield2]" /></td>{/if}
  <td class="FormButton" nowrap="nowrap">{$lng.lbl_custom_field_2}:</td>
  <td class="ProductDetails">
 {include file="main/textarea.tpl" name="cpfield2" cols=45 rows=8 data=$product.cpfield2 width="100%" btn_rows=4}
  </td>
</tr>
{* Custom Product Select Dropdown Field - cpfield3 *}
<tr> 
  {if $geid ne ''}<td width="15" class="TableSubHead"><input type="checkbox" value="Y" name="fields[cpfield3]" /></td>{/if}
  <td class="FormButton" nowrap="nowrap">{$lng.lbl_custom_field_3}:</td>
  <td class="ProductDetails">
  <select name="cpfield3">
 <option value='N'{if $product.cpfield3 eq 'N'} selected="selected"{/if}>{$lng.lbl_no}</option>
 <option value='Y'{if $product.cpfield3 eq 'Y'} selected="selected"{/if}>{$lng.lbl_yes}</option>
  </select> 
  {* If you prefer a checkbox, use this code instead of the select dropdown...
  <input type="checkbox" name="cpfield3" value="Y"{if $product.cpfield3 eq "Y"} checked="checked"{/if} />
  *}
  </td>
</tr>

Then upload the new file to skin/common_files/main


Then in skin/common_files/main/product_details.tpl

Wherever you want the fields to appear, eg. after...

{if $active_modules.New_Arrivals}
  {include file="modules/New_Arrivals/new_arrivals_product_modify_fields.tpl" product=$product}
{/if}

Insert...

{* Custom Product Fields *}
{include file="main/product_custom_fields.tpl"}
{* /Custom Product Fields *}

Next we need to allow inserting of the new fields into the database...

In include/product_modify.php

After...

 'title_tag' => $title_tag,

Insert...

 'cpfield1' => $cpfield1,
 'cpfield2' => $cpfield2,
 'cpfield3' => $cpfield3,

Next in order to allow HTML in the cpfield2 field, we need to make it a 'trusted' field.

In admin/product_modify.php

After...

 'efields',

Insert...

 'cpfield2',

Then in provider/product_modify.php

After...

 'efields'

Insert...

 ,'cpfield2'

Now we need to clear the X-Cart / template cache.

In Tools > Maintenance, under 'Force cache generation', click the 'Force cache generation' button.

In Tools > Maintenance, under 'Clear templates/X-Cart cache', click the 'Clear' button.

Done.


So having added some data to the new fields, we can start to use them in the store...

{if $product.cpfield1 ne ""}{$lng.lbl_custom_field_1}: {$product.cpfield1}{/if}
{if $product.cpfield2 ne ""}{$lng.lbl_custom_field_2}: {$product.cpfield2}{/if}

And...

{$lng.lbl_custom_field_3}: {if $product.cpfield3 eq "Y"}YUP{else}NOPE{/if}
The website uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy.
More Information