为 OpenCart 2.3.0.2 中的相关产品添加产品选项

Adding product options to related product in OpenCart 2.3.0.2

想法很简单;我想让我的客户在访问单个产品时更轻松地添加相关产品。我的相关产品有必需的选项,所以选项也需要显示。

我想我在展示部分很接近,不知道如何通过 ajax 调用等添加到购物车。请注意我不是程序员,我经常 fiddle PHP 代码所以我知道一点。

我开始在控制器中编辑; catalog/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']
                );



            }
        }

然后我在/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 个相关产品中获取所有选项。

我做错了什么?

真诚希望大家帮帮忙! 干杯, 大卫

尝试将 //getting options 代码移动到 $data['products'][] = array 上方,以便我们可以将其放入产品数组中。

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

然后,您可以使用 if ($product['options'])product.tpl 调用它(当然在 foreach ($products as $product) 内)。