Opencart 3获取最新产品视图中的产品选项

时间:2017-12-17 20:51:58

标签: php opencart opencart-3

我正在修改OpenCart默认主题,我想在最新的产品视图页面上获取产品选项。

我从产品视图页面复制了此代码:

{% if options %}
 <hr>
 <h3>{{ text_option }}</h3>

 {% for option in options %}

  {% if option.type == 'select' %}
  <div class="form-group{% if option.required %} required {% endif %}">
  <label class="control-label" for="input-option{{ option.product_option_id }}">{{ option.name }}</label>
  <select name="option[{{ option.product_option_id }}]" id="input-option{{ option.product_option_id }}" class="form-control">
  <option value="">{{ text_select }}</option>
  {% for option_value in option.product_option_value %}
  <option value="{{ option_value.product_option_value_id }}">{{ option_value.name }}
  {% if option_value.price %}
  ({{ option_value.price_prefix }}{{ option_value.price }})
   {% endif %} </option>
   {% endfor %}
  </select>
</div>
{% endfor %}
{% endif %}

我在catalog/controller/extention/module/latest.php

中复制了此代码
$data['options'] = array();

    foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
        $product_option_value_data = array();

        foreach ($option['product_option_value'] as $option_value) {
            if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
                if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
                    $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
                } else {
                    $price = false;
                }

                $product_option_value_data[] = array(
                    'product_option_value_id' => $option_value['product_option_value_id'],
                    'option_value_id'         => $option_value['option_value_id'],
                    'name'                    => $option_value['name'],
                    'image'                   => $this->model_tool_image->resize($option_value['image'], 50, 50),
                    'price'                   => $price,
                    'price_prefix'            => $option_value['price_prefix']
                );
            }
        }

        $data['options'][] = array(
            'product_option_id'    => $option['product_option_id'],
            'product_option_value' => $product_option_value_data,
            'option_id'            => $option['option_id'],
            'name'                 => $option['name'],
            'type'                 => $option['type'],
            'value'                => $option['value'],
            'required'             => $option['required']
        );
    }

我得到了

 Undefined index: product_id in F:\xampp\htdocs\*\catalog\controller\extension\module\latest.php

foreach参数

$this->request->get['product_id'])

以上product_idUndefined,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

以下是您需要替换的页面

  

目录\视图\主题\默认\模板\扩展\模块\ latest.twig

<h3>{{ heading_title }}</h3>
<div class="row"> {% for product in products %}
  <div class="product-layout col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <div class="product-thumb transition">
      <div class="image"><a href="{{ product.href }}"><img src="{{ product.thumb }}" alt="{{ product.name }}" title="{{ product.name }}" class="img-responsive" /></a></div>
      <div class="caption">
        <h4><a href="{{ product.href }}">{{ product.name }}</a></h4>
        <p>{{ product.description }}</p>
        {% if product.rating %}
        <div class="rating">{% for i in 1..5 %}
          {% if product.rating < i %} <span class="fa fa-stack"><i class="fa fa-star-o fa-stack-2x"></i></span> {% else %} <span class="fa fa-stack"><i class="fa fa-star fa-stack-2x"></i><i class="fa fa-star-o fa-stack-2x"></i></span> {% endif %}
          {% endfor %}</div>
        {% endif %}
        {% if product.price %}
        <p class="price"> {% if not product.special %}
          {{ product.price }}
          {% else %} <span class="price-new">{{ product.special }}</span> <span class="price-old">{{ product.price }}</span> {% endif %}
          {% if product.tax %} <span class="price-tax">{{ text_tax }} {{ product.tax }}</span> {% endif %} </p>
        {% endif %} 

        {% if product.options %}
         <hr>
         <h3>{{ text_option }}</h3>

         {% for option in product.options %}

          {% if option.type == 'select' %}
          <div class="form-group{% if option.required %} required {% endif %}">
          <label class="control-label" for="input-option{{ option.product_option_id }}">{{ option.name }}</label>
          <select name="option[{{ option.product_option_id }}]" id="input-option{{ option.product_option_id }}" class="form-control">
          <option value="">{{ text_select }}</option>
          {% for option_value in option.product_option_value %}
          <option value="{{ option_value.product_option_value_id }}">{{ option_value.name }}
          {% if option_value.price %}
          ({{ option_value.price_prefix }}{{ option_value.price }})
           {% endif %} </option>
           {% endfor %}
          </select>
        </div>
        {% endif %}
        {% endfor %}
        {% endif %}        
        </div>
      <div class="button-group">
        <button type="button" onclick="cart.add('{{ product.product_id }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
        <button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>
        <button type="button" data-toggle="tooltip" title="{{ button_compare }}" onclick="compare.add('{{ product.product_id }}');"><i class="fa fa-exchange"></i></button>
      </div>
    </div>
  </div>
  {% endfor %} </div>
  

目录\控制器\扩展\模块\ latest.php

<?php
class ControllerExtensionModuleLatest extends Controller {
    public function index($setting) {
        $this->load->language('extension/module/latest');

        $this->load->model('catalog/product');

        $this->load->model('tool/image');

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

        $filter_data = array(
            'sort'  => 'p.date_added',
            'order' => 'DESC',
            'start' => 0,
            'limit' => $setting['limit']
        );

        $results = $this->model_catalog_product->getProducts($filter_data);

        if ($results) {
            foreach ($results as $result) {
                if ($result['image']) {
                    $image = $this->model_tool_image->resize($result['image'], $setting['width'], $setting['height']);
                } else {
                    $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['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 = $result['rating'];
                } else {
                    $rating = false;
                }

                $options = array();

                foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
                    $product_option_value_data = array();

                    foreach ($option['product_option_value'] as $option_value) {
                        if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
                            if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
                                $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
                            } else {
                                $price = false;
                            }

                            $product_option_value_data[] = array(
                                'product_option_value_id' => $option_value['product_option_value_id'],
                                'option_value_id'         => $option_value['option_value_id'],
                                'name'                    => $option_value['name'],
                                'image'                   => $this->model_tool_image->resize($option_value['image'], 50, 50),
                                'price'                   => $price,
                                'price_prefix'            => $option_value['price_prefix']
                            );
                        }
                    }

                    $options[] = array(
                        'product_option_id'    => $option['product_option_id'],
                        'product_option_value' => $product_option_value_data,
                        'option_id'            => $option['option_id'],
                        'name'                 => $option['name'],
                        'type'                 => $option['type'],
                        'value'                => $option['value'],
                        'required'             => $option['required']
                    );
                }


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

            return $this->load->view('extension/module/latest', $data);
        }
    }
}

Result