2016-01-31 67 views
1

我正在嘗試添加多語言自定義產品字段。請有人幫我解決這個問題。添加自定義多語言產品字段

添加表

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

opencart版本? –

+0

我正在使用當前版本2.1.0.2 @zedBlackbeard – user12345

+0

問題即將到來? –

回答

0

檢查有以下變化:

添加表

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; 

在管理/視圖/模板/目錄/ 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> 

管理員/控制器/目錄/ 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'], 
       ); 
      } 
     } 

管理員/模型/目錄/產品。PHP

addProduct方法之前$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->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->addProduct($data);

之前添加以下代碼
$data['product_custom'] = $this->getProductCustoms($product_id); 

在方法中添加以下代碼之前$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_mediabox表。

+0

Awsm @zed,但我有一個問題,請參閱圖像鏈接:http://i68.tinypic.com/2ia4nra.jpg – user12345

+0

添加標題'language1' - 'language2'並保存後顯示我4輸入字段。 – user12345

+0

Fiels工作不正常。 – user12345

相關問題