【问题标题】:Add custom multi language product fields添加自定义多语言产品字段
【发布时间】:2016-01-31 15:33:23
【问题描述】:

我正在尝试添加多语言自定义产品字段。请任何人帮助我解决这个问题。

添加表格

CREATE TABLE IF NOT EXISTS `product_custom` (
      `product_id` int(11) NOT NULL,
      `title` varchar(255) CHARACTER SET utf8 NOT NULL,
      ` language_id` int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

在 admin/view/template/catalog/product.tpl

 <div class="tab-pane" id="tab-custom">
                      <div class="table-responsive">
                            <table id="custom" class="table table-striped table-bordered table-hover">
                              <thead>
                                <tr>
                                  <td class="text-right">Title</td>
                                  <td class="text-right">Value</td>
                                  <td></td>
                                </tr>
                              </thead>
                              <tbody>
                                <?php $custom_row = 0; ?>
                                <?php foreach ($product_customs as $product_custom) { ?>
                                <tr id="custom-row<?php echo $custom_row; ?>">
                                  <?php foreach ($languages as $language) { ?>
                                  <td class="text-right">
                                   <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" style="margin-right:10px;padding:5px 0px"/><br/>
                                   <input type="text" name="product_custom[<?php echo $custom_row; ?>][<?php echo $language['language_id']; ?>][title]" value="<?php echo $product_custom[$language['language_id']]['title']; ?>" placeholder="Title" class="form-control" />
                                 </td>
                                   <?php }?>
                                  <td class="text-left"><button type="button" onclick="$('#custom-row<?php echo $custom_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
                                </tr>
                                <?php $custom_row++; ?>
                                <?php } ?>
                              </tbody>
                              <tfoot>
                                <tr>
                                  <td colspan="1"></td>
                                  <td class="text-left"><button type="button" onclick="addCustom();" data-toggle="tooltip" title="Add Mediabox" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
                                </tr>
                           </tfoot>
                     </table>
               </div>
            </div>

<script type="text/javascript"><!--
        var custom_row = <?php echo $custom_row; ?>;

        function addCustom() {
            html  = '<tr id="custom-row' + custom_row + '">';
            <?php foreach ($languages as $language) { ?>
                html += '  <td class="text-right"><input type="text" name="product_custom[' + custom_row + '][title]" value="" placeholder="Title" class="form-control" /></td>';
            <?php }?>
            html += '  <td class="text-left"><button type="button" onclick="$(\'#custom-row' + custom_row + '\').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
            html += '</tr>';

              $('#custom tbody').append(html);

            custom_row++;
        }
        //--></script>

在 admin/controller/catalog/product.php 中

//Custom
                if (isset($this->request->post['product_custom'])) {
                    $product_customs = $this->request->post['product_custom'];
                } elseif (isset($this->request->get['product_id'])) {
                    $product_customs = $this->model_catalog_product->getProductCustoms($this->request->get['product_id']);
                } else {
                    $product_customs = array();
                }

                $data['product_mediaboxs'] = array();

                foreach ($product_customs as $language_id => $product_custom) {

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

                    $data['product_customs'][] = array(
                        'title'          => $product_custom['title'],
                        );
                }

在 admin/model/catalog/product.php 中

if (isset($data['product_custom'])) {
                    foreach ($data['product_custom'] as $language_id => $product_custom) {
                        $this->db->query("INSERT INTO " . DB_PREFIX . "product_custom SET product_id = '" . (int)$product_id . "', title = '" . $this->db->escape($product_custom['title']) . "', language_id = '" . (int)$language_id . "'");
                    }
                }


$this->db->query("DELETE FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int)$product_id . "'");

                if (isset($data['product_custom'])) {
                    foreach ($data['product_custom'] as $language_id => $product_custom) {
                        $this->db->query("INSERT INTO " . DB_PREFIX . "product_mediabox SET product_id = '" . (int)$product_id . "', title = '" . $this->db->escape($product_custom['title']) . "', language_id = '" . (int)$language_id . "'");
                    }
                }

$data['product_custom'] = $this->getProductCustoms($product_id);

$this->db->query("DELETE FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int)$product_id . "'");


public function getProductCustoms($product_id) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int)$product_id . "' ORDER BY title");

                return $query->rows;
            }

【问题讨论】:

  • 我正在使用当前版本 2.1.0.2 @zedBlackbeard
  • 问题来了?
  • 输入值不保存到数据表
  • 我手动将值添加到数据表中,但在 opencart 管理员中它显示&lt;b&gt;Notice&lt;/b&gt;: Undefined offset: 1
  • 没有多语言我的意思是当我删除所有$language['language_id'] 然后它作为非多语言工作正常,但我想添加多语言选项。抱歉英语不好..

标签: php e-commerce opencart shopping-cart opencart2.x


【解决方案1】:

检查以下更改:

添加表格

CREATE TABLE IF NOT EXISTS `oc_product_custom` (
  `opc_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  PRIMARY KEY (`opc_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `oc_product_custom_desc` (
  `opc_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `language_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

在 admin/view/template/catalog/product_form.tpl

在下面添加html

<div class="tab-pane" id="tab-custom">
              <div class="table-responsive">
                    <table id="custom" class="table table-striped table-bordered table-hover">
                      <thead>
                        <tr>
                          <td class="text-right">Title</td>
                          <td></td>
                        </tr>
                      </thead>
                      <tbody>
                        <?php $custom_row = 0; ?>
                        <?php foreach ($product_customs as $product_custom) { ?>

                        <tr id="custom-row<?php echo $custom_row; ?>"><td class="text-right">
                          <?php foreach ($languages as $language) { ?>
                          <div class="input-group"><span class="input-group-addon"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /></span>                           
                           <input type="text" name="product_custom[<?php echo $custom_row; ?>][<?php echo $language['language_id']; ?>][title]" value="<?php echo $product_custom[$language['language_id']]['title']; ?>" placeholder="Title" class="form-control" />
                          </div>
                           <?php }?>
                           </td>
                          <td class="text-left"><button type="button" onclick="$('#custom-row<?php echo $custom_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
                        </tr>
                        <?php $custom_row++; ?>
                        <?php } ?>
                      </tbody>
                      <tfoot>
                        <tr>
                          <td colspan="1"></td>
                          <td class="text-left"><button type="button" onclick="addCustom();" data-toggle="tooltip" title="Add Mediabox" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
                        </tr>
                   </tfoot>
             </table>
       </div>
    </div>

在下面添加script

<script type="text/javascript"><!--
var custom_row = <?php echo $custom_row; ?>;

function addCustom() {
    html  = '<tr id="custom-row' + custom_row + '">';
    html += '<td class="text-right">';
    <?php foreach ($languages as $language) { ?>
        html += '<div class="input-group"><span class="input-group-addon"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /></span><input type="text" name="product_custom[' + custom_row + '][<?php echo $language['language_id']; ?>][title]" value="" placeholder="Title" class="form-control" /></div>';
    <?php }?>
    html += '</td>';
    html += '  <td class="text-left"><button type="button" onclick="$(\'#custom-row' + custom_row + '\').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
    html += '</tr>';

      $('#custom tbody').append(html);

    custom_row++;
}
//--></script>

admin/controller/catalog/product.php

//Custom
        if (isset($this->request->post['product_custom'])) {
            $product_customs = $this->request->post['product_custom'];
        } elseif (isset($this->request->get['product_id'])) {
            $product_customs = $this->model_catalog_product->getProductCustoms($this->request->get['product_id']);
        } else {
            $product_customs = array();
        }

        $data['product_customs'] = array();

        foreach ($product_customs as $key=>$product_custom) {           

                foreach ($data['languages'] as $language){
                $customdata = $this->model_catalog_product->getProductCustom($product_custom['opc_id'],$language['language_id']);

                $data['product_customs'][$key][$language['language_id']] = array(
                    'title' => $customdata['title'],
                ); 
            }
        }

admin/model/catalog/product.php

addProduct方法中$this-&gt;cache-&gt;delete('product')之前添加以下代码

if (isset($data['product_custom'])) {
        foreach ($data['product_custom'] as $product_custom_data) {
            $this->db->query("INSERT INTO `" . DB_PREFIX . "product_custom` SET `product_id` = '" . (int) $product_id . "'");
            $opc_id = $this->db->getLastId();
            if (!empty($product_custom_data)) {
                foreach ($product_custom_data as $language_id => $product_custom) {
                    $this->db->query("INSERT INTO `" . DB_PREFIX . "product_custom_desc` SET `opc_id` = '" . (int) $opc_id . "', `title` = '" . $this->db->escape($product_custom['title']) . "', `language_id` = '" . (int) $language_id . "'");
                }
            }
        }
    }

editProduct 方法中,在$this-&gt;cache-&gt;delete('product') 之前添加以下代码

$this->db->query("DELETE FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int) $product_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "product_custom_desc WHERE product_id = '" . (int) $product_id . "'");
if (isset($data['product_custom'])) {
            foreach ($data['product_custom'] as $product_custom_data) {
                $this->db->query("INSERT INTO `" . DB_PREFIX . "product_custom` SET `product_id` = '" . (int) $product_id . "'");
                $opc_id = $this->db->getLastId();
                if (!empty($product_custom_data)) {
                    foreach ($product_custom_data as $language_id => $product_custom) {
                        $this->db->query("INSERT INTO `" . DB_PREFIX . "product_custom_desc` SET `opc_id` = '" . (int) $opc_id . "', `title` = '" . $this->db->escape($product_custom['title']) . "', `language_id` = '" . (int) $language_id . "'");
                    }
                }
            }
        }

copyProduct方法中$this-&gt;addProduct($data);之前添加以下代码

$data['product_custom'] = $this->getProductCustoms($product_id);

deleteProduct 方法中,在$this-&gt;cache-&gt;delete('product') 之前添加以下代码

$this->db->query("DELETE FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int) $product_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "product_custom_desc WHERE product_id = '" . (int) $product_id . "'");

添加以下方法:

public function getProductCustoms($product_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE product_id = '" . (int) $product_id . "'");

        return $query->rows;
    }

    public function getProductCustom($opc_id,$language_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom_desc WHERE opc_id = '" . (int) $opc_id . "' AND language_id='".$language_id."' ORDER BY title");

        return $query->row;
    }

注意:我认为不需要product_mediabox 表。

【讨论】:

  • Awsm @zed 但我有问题请看图片链接:i68.tinypic.com/2ia4nra.jpg
  • 添加标题后language1 - language2 并保存后显示4个输入字段。
  • 应该没有问题,因为这是经过测试的代码。 Check it out here : awesomescreenshot.com/00c5le2geb
  • 请问您可以添加完整的代码吗?发生了什么变化...因为我遵循您的更改但无法正常工作。谢谢
  • 我已经检查过了:见附件截图awesomescreenshot.com/00c5le2geb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多