我正在嘗試添加多語言自定義產品字段。請有人幫我解決這個問題。添加自定義多語言產品字段
添加表
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;
在管理/視圖/模板/目錄/ 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>
在管理/控制器/目錄/產品.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'],
);
}
在管理/模型/目錄/ 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;
}
opencart版本? –
我正在使用當前版本2.1.0.2 @zedBlackbeard – user12345
問題即將到來? –