如何在opencart 2.0的类别页面中添加产品选项

时间:2015-05-27 18:00:25

标签: php opencart opencart2.x

我想在opencart 2.0的类别页面中添加产品选项。

我在\ 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 }

然后在\ controller \ product \ category.php

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

所以现在在类别$ 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'])
);

现在我不明白如何在我的视图中调用它,即在collection.tpl中

请帮助我在我的视图中显示此内容,以在类别页面中显示产品选项。

1 个答案:

答案 0 :(得分:1)

我花了好几个小时做这件事,但我把它弄好了。现在,我只需要SELECT,所以你去修改它以满足你在category.tpl中的需要我正在使用版本2.1.0.2

1-转至/catalog/controller/product/category.php 然后>>找到产品阵列

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

2-在数组

上添加此代码
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-在product array

中添加此代码
'option'    => $option_data

4-转到/ catalog / view / theme /您的TEMPLATE / template / product / category.tpl并将此代码添加到产品循环中

        <?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 } ?>

我仍在使用javascript工具,因此我可以将数量和选项添加到购物车中,而无需进入产品页面。感谢