2017-08-20 15 views
0

アイデアは単純です。私は顧客が単一の製品を訪問している間に関連製品を簡単に追加できるようにしたいと考えています。私の関連商品にはオプションが必要なので、オプションも表示する必要があります。OpenCart 2.3.0.2で関連製品に製品オプションを追加

私は、Ajaxコールなどでカートに追加する方法を知らないと思います。私はプログラマーではないことに注意してください。

コントローラで編集を開始しました。 カタログ/コントローラ/製品/ product.php

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

     $results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']); 

     //adding the array that needs to be filled with product options 
     $data['daaf_options'] = array(); 

     foreach ($results as $result) { 

      if ($result['image']) { 
       $image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height')); 
      } else { 
       $image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height')); 
      } 

      if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 
       $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']); 
      } else { 
       $price = false; 
      } 

      if ((float)$result['special']) { 
       $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']); 
      } else { 
       $special = false; 
      } 

      if ($this->config->get('config_tax')) { 
       $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']); 
      } else { 
       $tax = false; 
      } 

      if ($this->config->get('config_review_status')) { 
       $rating = (int)$result['rating']; 
      } else { 
       $rating = false; 
      } 

      $data['products'][] = array(
       'product_id' => $result['product_id'], 
       'thumb'  => $image, 
       'name'  => $result['name'], 
       'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..', 
       'price'  => $price, 
       'special'  => $special, 
       'tax'   => $tax, 
       'minimum'  => $result['minimum'] > 0 ? $result['minimum'] : 1, 
       'rating'  => $rating, 
       'href'  => $this->url->link('product/product', 'product_id=' . $result['product_id']) 
      ); 


      //getting the options 

      $daaf_getoptions = $this->model_catalog_product->getProductOptions($result['product_id']); 

      $related_product_option_value_data = array(); 

      foreach ($daaf_getoptions as $related_option) { 

       foreach ($related_option['product_option_value'] as $related_option_value) { 

         $related_product_option_value_data[] = array(
          'product_option_value_id' => $related_option_value['product_option_value_id'], 
          'option_value_id'   => $related_option_value['option_value_id'], 
          'name'     => $related_option_value['name'] 
         ); 

       } 

       $data['daaf_options'][] = array(
        'product_option_id' => $related_option['product_option_id'], 
        'product_option_value' => $related_product_option_value_data, 
        'option_id'   => $related_option['option_id'], 
        'name'     => $related_option['name'], 
        'type'     => $related_option['type'], 
        'value'    => $related_option['value'], 
        'required'    => $related_option['required'] 
       ); 



      } 
     } 

それから私は、/catalog/view/theme/default/template/product/product.tpl

に関連製品にオプションを追加
  $product_options_center = $modules_old_opencart->getModules('product_options_center'); 
     if(count($product_options_center)) { 
     foreach ($product_options_center as $module) { 
      echo $module; 
     } 
     } ?> 



     <?php if ($daaf_options) { ?> 
     <div class="options"> 
     <?php foreach ($daaf_options as $related_option) { ?> 
     <?php if ($related_option['type'] == 'select') { ?> 
     <div class="form-group<?php echo ($related_option['required'] ? ' required' : ''); ?>"> 
      <label class="control-label" for="input-option<?php echo $related_option['product_option_id']; ?>"><?php echo $related_option['name']; ?></label> 
      <select name="option[<?php echo $related_option['product_option_id']; ?>]" id="input-option<?php echo $related_option['product_option_id']; ?>" class="form-control"> 
      <option value=""><?php echo $text_select; ?></option> 
      <?php foreach ($related_option['product_option_value'] as $related_option_value) { ?> 
      <option value="<?php echo $related_option_value['product_option_value_id']; ?>"><?php echo $related_option_value['name']; ?> 
      </option> 
      <?php } ?> 
      </select> 
     </div> 
     <?php } ?> 

     <?php } ?> 
     </div> 
     <?php } ?> 

これは今のようです。 http://imgur.com/a/RDS3V

この例の製品には7つの関連製品があり、すべてに1つの必須オプションがあります。すべての関連製品は7つの関連製品からすべてのオプションを取得しているので、明らかに間違っている。

私は間違っていますか?

誰でも手伝ってください。 乾杯、 デビッド

答えて

0

ので、我々は製品の配列の中に置くことができます$data['products'][] = array//getting optionsコードを移動するようにしてください。

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

    $results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']); 

    foreach ($results as $result) { 

     if ($result['image']) { 
      $image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height')); 
     } else { 
      $image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_related_width'), $this->config->get($this->config->get('config_theme') . '_image_related_height')); 
     } 

     if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) { 
      $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']); 
     } else { 
      $price = false; 
     } 

     if ((float)$result['special']) { 
      $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']); 
     } else { 
      $special = false; 
     } 

     if ($this->config->get('config_tax')) { 
      $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']); 
     } else { 
      $tax = false; 
     } 

     if ($this->config->get('config_review_status')) { 
      $rating = (int)$result['rating']; 
     } else { 
      $rating = false; 
     } 

     //adding the array that needs to be filled with product options 
     $daaf_options = array(); 

     //getting the options 

     $daaf_getoptions = $this->model_catalog_product->getProductOptions($result['product_id']); 

     $related_product_option_value_data = array(); 

     foreach ($daaf_getoptions as $related_option) { 

      foreach ($related_option['product_option_value'] as $related_option_value) { 

        $related_product_option_value_data[] = array(
         'product_option_value_id' => $related_option_value['product_option_value_id'], 
         'option_value_id'   => $related_option_value['option_value_id'], 
         'name'     => $related_option_value['name'] 
        ); 

      } 

      $daaf_options[] = array(
       'product_option_id' => $related_option['product_option_id'], 
       'product_option_value' => $related_product_option_value_data, 
       'option_id'   => $related_option['option_id'], 
       'name'     => $related_option['name'], 
       'type'     => $related_option['type'], 
       'value'    => $related_option['value'], 
       'required'    => $related_option['required'] 
      ); 



     } 

     $data['products'][] = array(
      'product_id' => $result['product_id'], 
      'thumb'  => $image, 
      'name'  => $result['name'], 
      'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..', 
      'price'  => $price, 
      'special'  => $special, 
      'tax'   => $tax, 
      'minimum'  => $result['minimum'] > 0 ? $result['minimum'] : 1, 
      'rating'  => $rating, 
      'href'  => $this->url->link('product/product', 'product_id=' . $result['product_id']), 
      'options'  => $daaf_options //we put the options here 
     ); 
    } 

その後、あなたは(当然のforeach ($products as $product)内)if ($product['options'])product.tplからそれを呼び出すことができます。

+0

私は十分なあなたに感謝することはできません@ボガラコン!それは魅力のように機能します! 今すぐカートに追加ボタンのもの、楽しい楽しみ:( –

関連する問題