Search code examples
phpopencartopencart-3

Opencart 3 get the product option in Latest Product view


I am modifying the OpenCart default theme and I want to get the the product options on the latest product view page.

I copied this code from the product view page:

{% 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 %}

I copied this code in 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']
        );
    }

And i get

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

In the foreach parameter

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

The above product_id is Undefined, Any suggestions?


Solution

  • Here is the pages you have to replace

    catalog\view\theme\default\template\extension\module\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>
    

    catalog\controller\extension\module\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