添加额外的产品标签(OpenCart)

时间:2016-03-30 08:54:07

标签: php opencart2.x

OpenCart版本2.1.0.2

示例是here。有3个产品标签(描述,评论,自定义标签)。

我需要添加另一个标签,可以分别从每个产品的管理面板添加信息。

  1. 我已添加到/catalog/view/theme/default/product/product.tpl

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

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

    $data['tab_video'] = $this->language->get('tab_video');
    
  4. 我在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>
    
  5. 现在,我在产品页面上看到该选项卡,在管理面板中看到该字段。但我有这个错误:

      

    注意:未定义的变量:第66行的/home/morganit/morgan-it.com.ua/italiavogs/admin/view/template/catalog/product_form.tpl中的entry_video

    如何将其连接到数据库以及如何使其正常工作?

1 个答案:

答案 0 :(得分:4)

首先,您需要在语言中定义变量$ entry_video以及这样的控制器:

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

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

2-控制器/admin/controller/catalog/product.php

行动getForm()

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

在product_description表

中添加1列名为video(type text)的列

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

在行动中getProductDescriptions()meta_keyword => $result['meta_keyword']

之后添加以下内容
'video'     => $result['video'],

现在/admin/model/catalog/product.php行动addProduct()&amp; editProduct()修改

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 - 查看

现在位于admin / view / template / catalog / product_form.tpl(通用标签行后面没有87-92)

   <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>

添加以下

<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>

现在只需通过调用列视频(来自product_description表)获取前端的自定义文本值&amp;将其显示在您的自定义标签中(在前端),只需遵循以下步骤:

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

添加

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

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

添加

$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

添加

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 /

添加

   <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>