2017-08-20 2 views
0

Die Idee ist einfach; Ich möchte es meinen Kunden erleichtern, verwandte Produkte hinzuzufügen, während sie ein einzelnes Produkt besuchen. Meine verwandten Produkte erfordern Optionen, so dass die Optionen auch angezeigt werden müssen.Hinzufügen von Produktoptionen zu verwandten Produkten in OpenCart 2.3.0.2

Ich denke, ich bin nah auf dem Vorführteil, keine Idee, wie man den Add-In-Wagen mit Ajax-Aufrufen etc. macht. Bitte beachten Sie, ich bin kein Programmierer, ich oft mit PHP-Code geifern, damit ich ein bisschen weiß.

Ich begann in der Steuerung zu bearbeiten; Katalog/Controller/product/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'] 
       ); 



      } 
     } 

Dann füge ich die Optionen auf die betreffenden Produkte in /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 } ?> 

So sieht es jetzt aus; http://imgur.com/a/RDS3V

Das Produkt im Beispiel hat 7 verwandte Produkte und sie alle haben 1 erforderliche Option. Offensichtlich geht es schief, denn jedes verwandte Produkt erhält alle Optionen aus den 7 verwandten Produkten.

Was mache ich falsch?

Mit freundlichen Grüßen hoffe, dass jeder helfen kann! Cheers, David

Antwort

0

Versuchen Sie, die //getting options Codes über dem $data['products'][] = array zu bewegen, so können wir es innerhalb Produktpalette setzen.

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

Dann können Sie es von product.tpl mit if ($product['options']) (innen foreach ($products as $product) natürlich) nennen.

+0

Ich kann dir @bogalakon nicht genug genug danken! Es wirkt wie ein Zauber! jetzt auf die in den Warenkorb Button Zeug, Spaß Spaß :( –

Verwandte Themen