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

Add custom multi language product fields

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

Add table

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;

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

In 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'],
                        );
                }

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

检查以下更改:

Add table

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;

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

注意:我认为product_mediaboxtable没有必要。