添加自定义多语言产品字段

时间:2016-01-31 15:33:23

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

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

  

添加表格

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

1 个答案:

答案 0 :(得分:0)

检查以下更改:

  

添加表格

java myfile
  

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

添加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; 以下

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
  

管理器/控制器/目录/ product.php

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

系统管理员/模型/目录/ 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'], ); } } 方法中的

addProduct

之前添加以下代码
$this->cache->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->cache->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->addProduct($data);
$data['product_custom'] = $this->getProductCustoms($product_id); 方法中的

deleteProduct

之前添加以下代码
$this->cache->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; } 表。