如何为产品中的每个选项添加购买按钮

时间:2020-08-10 06:48:07

标签: php opencart opencart-3

如何为类别页面上的每个选项添加购买按钮?在Opencart 3中

/template/product/category.twig:

{% if product.options %}
       <div class="product__prices prices">
        {% for option in product.options %}
        {% if option.type == 'radio' %}
        <div class="form-group{% if option.required %} required {% endif %}">
          {# <label class="control-label">{{ option.name }}</label> #}
          <div  id="input-option{{ option.product_option_id }}"> 
          {% for option_value in option.product_option_value %}
            <div class="prices__row">
            <div class="prices__info">
                    <div class="prices__name">{{ option_value.name }}</div>
                    <div class="prices__weight">{{ option_value.weight }}гр</div>
                </div>
                <div class="prices__line"></div>
                <div class="prices__price">
                  {% if option_value.price %}
                     {{ option_value.price }}
                  {% endif %}
                </div>
                    <a href="#" class="prices__buy"><span>Купить</span></a>
              <label>
                {# <input type="radio" name="option[{{ option.product_option_id }}]" value="{{ option_value.product_option_value_id }}" />
                {% if option_value.image %} <img src="{{ option_value.image }}" alt="{{ option_value.name }} {% if option_value.price %} {{ option_value.price_prefix }} {{ option_value.price }} {% endif %}" class="img-thumbnail" /> {% endif %}                  
                {{ option_value.name }}
                {% if option_value.price %}
                ({{ option_value.price_prefix }}{{ option_value.price }})
                {% endif %} </label> #}
            </div>
            {% endfor %} 
          </div>
        </div>
        {% endif %}
        {% endfor %}
        </div>
      {% endif %}

我在控制器中注册了产品选项数据的输出,但是如何为他们的每个选项添加购买按钮,以便在单击它时将产品添加到购物车中?

目录/控制器/产品/category.php:

$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'], $result['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,
                        'weight'                  => round($option_value['weight'],0),
                        '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,
                'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
                'rating'      => $result['rating'],
                'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
            );
        }

0 个答案:

没有答案