How to add product options in category page in ope

2019-07-21 04:24发布

I want to add product options in the category page in opencart 2.0.

I have added this code in \catalog\model\catalog\product.php

public function hasOptionPriceIncrease($product_id) {
   $option_data = $this->getProductOptions($product_id);
if (is_array($product_option_value)) {
  foreach ($product_option_value as $option) {
  if (is_array($option['product_option_value'])) {
    foreach ($option['product_option_value'] as $value) {
      if ($value['price'] > 1) {
        return true;
      }
    }
  }
} return false }

and then in \controller\product\category.php

'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])

so now in category $this->data['products'][] becomes something like this-

$this->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, 100) . '..',
'price'       => $price,
'special'     => $special,
'tax'         => $tax,
'rating'      => $result['rating'],
'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])
);

Now i didn't understand how to call this in my view i.e., in collection.tpl

Please help me to display this in my view, to show the product options in category page.

1条回答
女痞
2楼-- · 2019-07-21 05:15

I spent hours doing this but I got it working. Now, I only needed the SELECT so you go and modified it to your needs in the category.tpl I'm using version 2.1.0.2

1- Go to /catalog/controller/product/category.php Then >> Find the product array

$data['products'][] = array(
                'product_id'  => $result['product_id'],

2- Add this code above the array

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

                                    foreach ($option['product_option_value'] as $option_value) {

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

                $option_data[] = 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']
                );
            }

3- add this code inside the products array

'option'    => $option_data

4- Go to /catalog/view/theme/YOUR TEMPLATE/template/product/category.tpl and add this code to your product loop

        <?php foreach ($product['option'] as $option) { ?>
         <?php if ($option['type'] == 'select') { ?>
          <select name="option[<?php echo $option['product_option_id']; ?>]" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control">
            <option value="">SELECT EXTENDED LENGTH</option>
            <?php foreach ($option['product_option_value'] as $option_value) { ?>
            <option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
            <?php if ($option_value['price']) { ?>
            (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
            <?php } ?>
            </option>
            <?php } ?>
          </select>
            <?php } ?>
        <?php } ?>

I'm still working in the javascript so I can add product to the cart with quantity and options without going into the product's page. Thanks

查看更多
登录 后发表回答