Page 1 of 3

Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Jul 22, 2011 12:56 am
by heizo
So I searched and did not find any resources on this, so I will type one up.

You will need MySql Knowledge and some php knowledge

Step 1.
First thing first, add your new fields to the product table in the database - this should be the easy part.

Step 2.
Open up product.php in admin/controller/catalog, you will want to copy and paste this piece of code. inside the getForm functions, (around line 707)

Code: Select all

if (isset($this->request->post['Table Name'])) {
      		$this->data['Table Name'] = $this->request->post['Table Name'];
    	} elseif (isset($product_info)) {
			$this->data['Table Name'] = $product_info['Table Name'];
		} else {
      		$this->data['Table Name'] = '';
    	}
also you will want to add the language variables like so (around line 521)

Code: Select all

$this->data['entry_table name'] = $this->language->get('entry_table name');
I kept my post names the same as my table names so as not to confuse myself, I would suggest doing the same.
For every field you want, copy and paste that code.

Step 3.
Open up the product_form.tpl file locate admin/view/template/catalog/.
This is pretty easy, just find a spot in the table and plug in your form fields. Look for the tab makers to tell what page you are adding to.

Code: Select all

<div id="tab-data">
This would be the data tab, you can figure out the names of the other ones.

I will give a couple of examples, here is a drop down

Code: Select all

<tr>
              <td><?php echo $entry_Table Name; ?></td>
              <td><select name="Table Name">
                  <?php foreach ($Table Names as $Table Name) { ?> // This is just looping over an array I built in the previouse page (step 2) and now loops here
                  <?php if ($Table Name['Table Name_id'] == $Table Name) { ?>
                  <option value="<?php echo $Table Name['Table Name_id']; ?>" selected="selected"><?php echo $Table Name['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $Table Name['Table Name_id']; ?>"><?php echo $Table Name['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select></td>
            </tr>
And here is just a regular input box:

Code: Select all

            <tr>
              <td><?php echo $entry_Table name; ?></td>
              <td><input type="text" name="Table name" value="<?php echo $Table name; ?>" /></td>
              </td>
            </tr>
Step 4.
Now open up the product.php in admin/language/english/catalog/

Add an entry

Code: Select all

$_['entry_Table Name']              = 'Descripton of field:';
Step 5.
Here is the tricky part, you will have to edit 2 queries in admin/model/catalog/product.php.

You want to edit in 2 places:

Code: Select all

public function addProduct($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', artist = '" . $this->db->escape($data['artist']) . "', bioLink = '" . $this->db->escape($data['bioLink']) . "', relatedCategory = '" . $this->db->escape($data['relatedCategory']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");

and here:

Code: Select all

public function editProduct($product_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', sku = '" . $this->db->escape($data['sku']) . "', artist = '" . $this->db->escape($data['artist']) . "', bioLink = '" . $this->db->escape($data['bioLink']) . "', relatedCategory = '" . $this->db->escape($data['relatedCategory']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");

Now that the admin side is done, we have to get this information on the other side... the user side.

Step 1.
Open up product.php in catalog/controller/product.

Add your language lines like so (around line 139)

Code: Select all

$this->data['entry_table name'] = $this->language->get('entry_table name');
Next add in your variable names containing the data (line 186ish)

Code: Select all

$this->data['table name'] = $product_info['table name'];
Step 2.
Open Up product.php in catalog/language/english/product/

Add your new names

Code: Select all

$_['text_table name']       = 'table name';

Step 3.

Open product.php in catalog/model/catalog/

Update the getProduct function, the mysql statement on line 14. Your prefix for your table names will be p.table name

Then add your entries to the array on line 18

Code: Select all

'table name'       => $query->row['table name'],
Step 4.
Add your fields to the product page in your template.

Finished :)

Cheers

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Jul 22, 2011 4:22 am
by justinv
Nice work. This is how it should be done.

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Aug 05, 2011 10:50 pm
by okmarket
Hi Jimmy,

for admin/model/catalog/product.php file which I found it's big differ with your file, so i att'd it, so can you help me add your code to my file, this is key step. thank you.

would you like to send back to me by email at garmentmill@gmail.com, thanks.

b.rgds
David

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Sat Aug 06, 2011 1:02 am
by JIM2Y
Hi David,

I just according heizo teaching, Changed two part of the CODE, you can try it
And thank you for you , my file is not in the original file, is added a Category Filter, I will fix it

My English is poor, please forgive.

Jimmy

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Sat Aug 06, 2011 7:57 am
by okmarket
Hi Jimmy, Heizo

Thank you for your help. now it's ok.

m/w, welcome to Guangzhou,China.

have a nice day

David

www.okmarket.com

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Mon Aug 08, 2011 8:35 pm
by memi
Hi,

thanks. nice post.

in relation to these changes, i would like to ask:

how would suggest to handle a system upgrade, say i would like in a few month to move to 1.5.5 or 1.6.1 or whatever version would be available then?

will i need to just do the same changes again in the new version or is there an automatic way to upgrade?

Best regards,
Memi.

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Mon Aug 15, 2011 10:06 pm
by werepair
hi, i have installed this but i am not showing the text on the product page, i have double checked everything and it all looks ok, i have the text field in my admin but it is not showing on my product pages.
1 thing
Step 4.
Add your fields to the product page in your template. ( what does this mean , i am not using the default theme do i have to do anything diffrent
if so please explain
best regards

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Wed Aug 17, 2011 8:36 am
by spitos
Can anyone help me with one of the steps in the instructions please?

I'm not to sure what to change here:
Step 3.

Open product.php in catalog/model/catalog/

Update the getProduct function, the mysql statement on line 14. Your prefix for your table names will be p.table name
My table is called 'warranty'

The query on line 14 is:

Code: Select all

$query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND customer_group_id = '" . (int)$customer_group_id . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
Also, when i go to view the product in admin, there is no data in the field i've setup, even though this data is in the database. How can I make it show here? Have i missed something?

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Wed Aug 17, 2011 9:25 pm
by spitos
Fixed the problem... a combination of late night and conflict with another vqmod!

Great work, thanks Heizo :good:

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Aug 19, 2011 10:51 am
by heizo
Thanks spitos - glad you got it fixed :) I guess my email isn't letting me know when people post back... but il make sure to check more often.

@werepair
It doesn't matter which theme you are using, to add your new fields to the product page the variable to use will most likely be <?php echo $tablename ?> if you followed the guide correctly. Place this where you want it to appear. Let me know if you still have questions.

@memi
if 1.6 were to come out, I would probably write new tutorials. Having to go through these steps again is very likely when upgrading as these change core files, and if an upgrade makes a change to any of these files then you are stuck with having to do this again... Such are the problems with changing a prebuilt cart. But lukily, this mod only takes about an hour if you know what you are doing... and it will be here for your reference.

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Sat Aug 27, 2011 10:51 pm
by tobychin
Ok. I've got most of this coded in. I am pretty new to OC and PHP and know I have a lot to learn. I've gotten as far as store/catalog/model/catalog/product.php and I'm stuck there. I'm using OC v1.4.9.4 and am sure that's why I can't get any farther. What and how do I edit this file to show my custom fields?

Thanks in advance

http://jbsfurniture.com/store/

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Sat Aug 27, 2011 11:27 pm
by tobychin
Never mind. I got it. Thanks for the great tutorial heizo! Great work!

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Thu Sep 01, 2011 4:03 pm
by united
When updating OC to a newer release, you can greatly simplify this process by using a revision control system like Subversion or Git, to automatically merge the changes from the latest OC. Then you will most likely not need to redo the above changes (unless those specific areas in the code were substantially modified in the newer OC revision).

Since OC already uses Subversion (in a repository hosted by googlecode), you can use Tortoise SVN (free windows program) to checkout OC's latest code from their Subversion repository http://opencart.googlecode.com/svn/. Then when they come out with a new feature or bug fixes you need, periodically do another checkout, and each time you do that SVN will automatically merge all the files combining both your changes and OC's changes! ;D In the event that some of the same lines changed and you need to resolve the conflict SVN alerts you to any such lines and lets you pick which lines to use in its built-in diff editor. (A Git RCS tool can also be used as they can be made to work with Subversion repositories.)

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Thu Sep 01, 2011 9:42 pm
by Maansy
For a fresh install, vqmod would be so handy to add one or more fields on top of manually alter the database.
Thanks for the tutorial ;)

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Sep 02, 2011 9:24 pm
by clawzeeb
Thank you for this post heizo. For adding a new custom field for a product, I did the same as you for the admin part.

However, for the catalog part, I want my custom field to be present only on products' description listed only on the category template - not on product template. In which files and/or functions should Ideclare my variable for this particular case? Thank you in advance.

Add Custom title To more Images of Products OpenCart 1.5.1

Posted: Fri Sep 09, 2011 11:31 pm
by body
Tell me how to add a description or title for additional product images.

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Mon Oct 17, 2011 4:03 pm
by okmarket
For 1.5.1.3 product.php file which I don't know how to modifty the code, since it's big difference. thank you.

<file name="catalog/model/catalog/product.php">
<operation error="skip">
<search position="after"><![CDATA[
'model' => $query->row['model'],
]]></search>
<add><![CDATA[
'size' => $query->row['size'],
]]></add>
</operation>
</file>

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Tue Nov 22, 2011 3:36 am
by helixconcepts
Using version 1.5.1.3
Went through instructions posted here and extension: http://www.opencart.com/index.php?route ... order=DESC
Works fine for one language, have only one input under catalog ->product ->data
Does any one have any ideas how to allow for text (input) to be shown in more than one language, EN and FR. :choke:
i.e.
English Features: This has XYZ
French Caractéristiques : Ce produit a des caractéristiques xyz
Thanks

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Feb 03, 2012 12:31 am
by remcofaasse
hi,
I am also looking for adding an extra custom field to products which is usable with multi languages.
All ideas are welcome.
Thanks!

Re: Add Custom Fields To Products OpenCart 1.5.1

Posted: Fri Feb 24, 2012 6:17 pm
by ann23262
Hi there, please can someone help me. I am stuck at this level and cannot figure out what change to make.

Step 3.

Open product.php in catalog/model/catalog/

Update the getProduct function, the mysql statement on line 14. Your prefix for your table names will be p.table name

My line 14 is:
$query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND customer_group_id = '" . (int)$customer_group_id . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews, p.sort_order FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");

Please could someone help.
Kind Regards
Annie