Crtvca Mag
Crtvca Mag

Reputation: 83

Create Custom Field in OpenCart 2.0

I am new to OpenCart and I would like to know how to add a custom field (a textarea with WYSIWYG editor enabled) in OpenCart back-end (in Product section) and then echo it to front-end (product.tpl).

I've followed this tutorial & successful created the custom field in backend admin panel but couldn't echo it to frontend.

Upvotes: 1

Views: 7012

Answers (2)

Aarif Nabi Roshangar
Aarif Nabi Roshangar

Reputation: 29

open admin\model\catalog\product.php

Find addProducts($data) 
function and add below code just after 
`sort_order`
and before     
date_added = NOW()");
your_desc = '" . $this->db->escape($data['your_desc']) . "',

Now find 
editProduct($product_id, $data)` function and add below code just after sort_order and before date_modified = NOW()
your_desc = '" . $this->db->escape($data['your_desc']) . "',

Now open admin\controller\catalog\product.php
Find getForm() function and add below code:

$data['entry_your_desc'] = $this->language->get('entry_your_desc');

Find below code 
if (isset($this->request->post['isbn'])) {
        $data['isbn'] = $this->request->post['isbn'];
    } elseif (!empty($product_info)) {
        $data['isbn'] = $product_info['isbn'];
    } else {
        $data['isbn'] = '';
    }

and add your code just after it

if (isset($this->request->post['your_desc'])) {
 $data['your_desc'] = $this->request->post['your_desc'];
} elseif (isset($product_info)) {
$data['your_desc'] = $product_info['your_desc'];
} else {
$data['your_desc'] = '';
}

Open catalog\view\theme\default\template\product\product.tpl
and find the below code:

<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
Now add your code after it:
 <li><?php echo $text_your_desc; ?> <?php echo $your_desc; ?></li>
You can place wherever you want to.

Open catalog\language\english\product\product.php
and add your piece of code:

$_['text_your_desc']               = 'Your_description';

Open catalog\controller\product\product.php
Find:

$data['heading_title'] = $product_info['name'];
and add:

$data['text_your_desc'] = $this->language->get('text_your_desc');

Scroll down and find

$data['product_id'] = (int)$this->request->get['product_id'];

now add:

$data['your_desc'] = $product_info['your_desc'];

Open admin\view\template\catalog\product_form.tpl
Find:

<div class="tab-pane" id="tab-data">

Now add your code:

<div class="form-group">
<label class="col-sm-2 control-label" for="input-your_desc">
<?php echo $entry_your_desc; ?>
</label>
<div class="col-sm-10">
<input type="text" name="your_desc" value="<?php echo $your_desc; ?>"      placeholder="<?php echo $entry_your_desc; ?>" id="input-your_desc" class="form- control" />
</div>
</div>

Open catalog\model\catalog\product.php
Find:

if ($query->num_rows) {
and add your code:

'your_desc'            => $query->row['your_desc'],

Open admin\language\english\catalog\product.php
Find :

//Entry
and add :

$_['entry_your_desc']      = 'Your_description';

Upvotes: 0

CLECode
CLECode

Reputation: 158

Hi, I found this tutorial which is compatible with Opencart 2.0:

http://www.php-dev-zone.com/2015/01/how-to-add-custom-product-field-in.html

Please refrain from overwriting core files and use vqmod instead:

http://www.opencart.com/index.php?route=extension/extension/info&extension_id=19501

I would advise you to use the tutorial and write everything into the actual core files first to see if it works (which seems correct) and then do it the proper way and move it all to vqmod.

In: /catalog/controller/product/product.php

You'll find things like:

$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');

If you add your field there, eg:

$data['custom_desc'] = $product_info['custom_desc'];

Then on the frontend you will be able to echo it into

/catalog/view/theme/default/template/product/product.tpl

as

$custom_desc;

Upvotes: 2

Related Questions