2

I want to customize the product description page of my site which is based on OpenCart by adding an additional field, but i'm unable to fined the file that describes the description page. I have checked the catalog/view/theme/(mytemplate)/product/product.twig file but that's not the file for my product description page.

focus.style
  • 6,612
  • 4
  • 26
  • 38
karuna
  • 59
  • 1
  • 10
  • Actually it is. And of course corresponding controller file. Just after you edit your files, do not forget refresh modifications and clear cache in admin dashboard. All data which you want retrieve in template, must be defined in corresponding controller file. – K. B. Apr 21 '20 at 11:54
  • @K.B. i have cleared the cache in admin dashboard and browser. I only want to add a custom section in the page. – karuna Apr 21 '20 at 12:14
  • Custom section, that you want to edit from admin panel? Like a second description? Write more details please. – focus.style Apr 21 '20 at 14:42
  • i want to add a sub title below of the product title. I have searched for the extension for the same but could not find anything. – karuna Apr 22 '20 at 05:48
  • ok. i see. will write down a detailed answer for you, you should edid 6 files and Database. But don't worry. I will describe everything. – focus.style Apr 22 '20 at 12:27

2 Answers2

4

Adding new field in product description OpenCart 3.X (multilingual).

On the Dashboard of Admin at the right top corner click on Gear icon. In the popup window turn the Cache OFF (both) and Refresh. This is necessary so that the results of your editing can be seen.

1. First of all, you need to add a new field in your database. Using phpMyAdmin, open your DB, find table oc_product_description (oc_ - is my prefix, use yours).

When you open oc_product_description, open SQL tab and write (don't forget to change oc_ to your DB prefix):

ALTER TABLE `oc_product_description` ADD `subtitle` VARCHAR(255) NOT NULL AFTER `name`;

This will add a new column subtitle.

2. Edition admin model admin/model/catalog/product.php

Find (≈12 line)

foreach ($data['product_description'] as $language_id => $value) {
  $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}

Replace with

foreach ($data['product_description'] as $language_id => $value) {
  $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', subtitle = '" . $this->db->escape($value['subtitle']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}

Find (≈146 line)

foreach ($data['product_description'] as $language_id => $value) {
  $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}

Replace with

foreach ($data['product_description'] as $language_id => $value) {
  $this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', subtitle = '" . $this->db->escape($value['subtitle']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
}

Find (≈432 line)

$product_description_data[$result['language_id']] = array(
  'name'             => $result['name'],

Add below

  'subtitle'         => $result['subtitle'],

3. Edition admin model admin/controller/catalog/product.php

Find (≈500 line)

if (isset($this->error['name'])) {
  $data['error_name'] = $this->error['name'];
} else {
  $data['error_name'] = array();
}

Add below

if (isset($this->error['subtitle'])) {
  $data['error_subtitle'] = $this->error['subtitle'];
} else {
  $data['error_subtitle'] = array();
}

Find

if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 255)) {
  $this->error['name'][$language_id] = $this->language->get('error_name');
}

Add below

if (utf8_strlen($value['subtitle']) > 255) {
  $this->error['subtitle'][$language_id] = $this->language->get('error_subtitle');
}

4. Editing admin language admin/language/en-gb(or your admin language)/catalog/product.php

Add at the end

$_['entry_subtitle']         = 'Product Subtitle';
$_['error_subtitle']         = 'Product Subtitle must less than 255 characters!';

5. Edition admin view admin/view/template/catalog/product_form.twig

Find (≈50 line)

<div class="form-group required">
  <label class="col-sm-2 control-label" for="input-name{{ language.language_id }}">{{ entry_name }}</label>
  <div class="col-sm-10">
    <input type="text" name="product_description[{{ language.language_id }}][name]" value="{{ product_description[language.language_id] ? product_description[language.language_id].name }}" placeholder="{{ entry_name }}" id="input-name{{ language.language_id }}" class="form-control"/>
    {% if error_name[language.language_id] %}
      <div class="text-danger">{{ error_name[language.language_id] }}</div>
    {% endif %} </div>
</div>

Add below

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-name{{ language.language_id }}">{{ entry_subtitle }}</label>
  <div class="col-sm-10">
    <input type="text" name="product_description[{{ language.language_id }}][subtitle]" value="{{ product_description[language.language_id] ? product_description[language.language_id].subtitle }}" placeholder="{{ entry_subtitle }}" id="input-subtitle{{ language.language_id }}" class="form-control"/>
    {% if error_subtitle[language.language_id] %}
      <div class="text-danger">{{ error_subtitle[language.language_id] }}</div>
    {% endif %} </div>
</div>

Now we can see new field in Admin panel while edition or adding a product.

Finally, we are going to edit customer side.

6. Edition customer model catalog/model/catalog/product.php

Find (≈10 line)

if ($query->num_rows) {
  return array(
    'product_id'       => $query->row['product_id'],
    'name'             => $query->row['name'],

Add below

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

7. Edition customer controller catalog/controller/product/product.php

Find (≈228 line)

$data['heading_title'] = $product_info['name'];

Add below

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

8. Editing customer view catalog/view/theme/default(or your theme)/template/product/product.twig

Find

<h1>{{ heading_title }}</h1>

Add below

<h3>{{ subtitle }}</h3>

Congrats! We have done it! After all thous manipulations you have to clear ocmod cache. Go to Admin, Extensions - Modifications, right top corner button Refresh. Than go back to admin Dashboard, click on right top corner Gear icon. In the popup window turn the Cache ON (both) and Refresh it. More about cache OpenCart 3.X cache cleaning here https://stackoverflow.com/a/61524855/3187127

This is suitable for any OpenCart 3.X, but in in some older 3.X versions you will have to clear twig cache by cleaning storage in file system.

focus.style
  • 6,612
  • 4
  • 26
  • 38
0

When editing a .twig file in OpenCart 3.x you will often face an issue with Twig Cache. You would edit the file and not see any changes.

To avoid this, 1. go to your admin panel 2. in the top right corner click on the gear icon. 3. turn off twig cache.

let me know if this helps?

Dmitriy Zhuk
  • 757
  • 1
  • 6
  • 9