简体   繁体   中英

Add extra product tab (OpenCart)

OpenCart version 2.1.0.2

Example is here . There are 3 product tab (Description, reviews, custom tab).

I need to add another tab with possibility to add information from admin panel for each product separately.

  1. I've added to /catalog/view/theme/default/product/product.tpl

     <li><a href="#tab-video" data-toggle="tab"><?php echo $tab_video; ?></a></li> 
  2. To /catalog/language/*/product/product.php

     $_['tab_video'] = 'Video'; 
  3. To /catalog/controller/product/product.php

     $data['tab_video'] = $this->language->get('tab_video'); 
  4. I added code to admin/view/template/catalog/product_form.tpl

     <label class="col-sm-2 control-label" for="input-video<?php echo $language['language_id']; ?>"><?php echo $entry_video; ?></label> <div class="col-sm-10"> <textarea name="product_video[<?php echo $language['language_id']; ?>][video]" placeholder="<?php echo $entry_video; ?>" id="input-video<?php echo $language['language_id']; ?>"> <?php echo isset($product_video[$language['language_id']]) ? $product_video[$language['language_id']]['video'] : ''; ?> </textarea> </div> 

Now I see that tab on product page and the field in admin panel. But I have this error:

Notice: Undefined variable: entry_video in /home/morganit/morgan-it.com.ua/italiavogs/admin/view/template/catalog/product_form.tpl on line 66

How can I connect it to database and how to make it works properly?

First of all you need to define variable $entry_video in language as well as controler like that :

1 - language /admin/language/*/catalog/product.php

$_['entry_video'] = 'Video';

2- controller /admin/controller/catalog/product.php

In action getForm()

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

add 1 column named video(type text) in product_description table

3 - model /admin/model/catalog/product.php

in action getProductDescriptions() add following after meta_keyword => $result['meta_keyword']

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

Now in /admin/model/catalog/product.php for action addProduct() & editProduct() modify

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']) . "'");
        }

AS

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']) . "', video = '" . $this->db->escape($value['video']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
        }    

4 - View

Now in admin/view/template/catalog/product_form.tpl (General Tab line near no 87-92) after

   <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-tag<?php echo $language['language_id']; ?>"><span data-toggle="tooltip" title="<?php echo $help_tag; ?>"><?php echo $entry_tag; ?></span></label>
                    <div class="col-sm-10">
                      <input type="text" name="product_description[<?php echo $language['language_id']; ?>][tag]" value="<?php echo isset($product_description[$language['language_id']]) ? $product_description[$language['language_id']]['tag'] : ''; ?>" placeholder="<?php echo $entry_tag; ?>" id="input-tag<?php echo $language['language_id']; ?>" class="form-control" />
                    </div>
                  </div>

Add Following

<div class="form-group">
                    <label class="col-sm-2 control-label" for="nput-video<?php echo $language['language_id']; ?>"><?php echo $entry_video; ?></label>
                    <div class="col-sm-10">
                      <textarea name="product_description[<?php echo $language['language_id']; ?>][video]" rows="5" placeholder="<?php echo $entry_video; ?>" id="input-video<?php echo $language['language_id']; ?>" class="form-control"><?php echo isset($product_description[$language['language_id']]) ? $product_description[$language['language_id']]['video'] : ''; ?></textarea>
                    </div>
                  </div>

Now simply you can get custom text values for frontend by calling column video (from product_description table) & dispalyed it to in your custom tab(on frontend) for that just follow following steps :

1- /catalog/language/*/product/product.php/

Add

$_['tab_video'] = 'Video';

2- /catalog/controller/product/product.php

Add

$data['product_video'] = $this->model_catalog_product->getProductDescriptions($this->request->get['product_id']);
                $this->load->model('localisation/language');
                $data['languages'] = $this->model_localisation_language->getLanguages();

3 - /catalog/model/catalog/product.php

Add

public function getProductDescriptions($product_id) {
    $product_description_data = array();

    $query = $this->db->query("SELECT video,language_id FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");

    foreach ($query->rows as $result) {
        $product_description_data[$result['language_id']] = array(
            'video'            => $result['video'],   
        );
    }

    return $product_description_data;
}

4 - /catalog/view/theme/*/default/template/product/product.tpl/

Add

   <div class="tab-pane" id="tab-video">
                <label class="col-sm-2 control-label" for="input-video<?php echo $language['language_id']; ?>"><?php echo $tab_video; ?></label>
<div class="col-sm-10">
  <textarea name="product_video[<?php echo $language['language_id']; ?>][video]"  id="input-video<?php echo $language['language_id']; ?>">
  <?php foreach($languages as $language) { ?>
   <?php echo isset($product_video[$language['language_id']]) ? $product_video[$language['language_id']]['video'] : ''; ?>
  </textarea>
</div>
<?php } ?>
                </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM