如何在Opencart 2.3的分类页面中添加选项

如何在Opencart 2.3的分类页面中添加选项,opencart,categories,options,opencart2.x,Opencart,Categories,Options,Opencart2.x,我已将产品选项添加到类别页面。工作正常,但当我从分类页面点击“添加到购物车”按钮时,它总是将我重定向到产品页面。所以我改变了“添加到购物车”按钮的行为,现在它调用了我的新Javascript函数。但看起来我的脚本或逻辑有问题。需要这方面的帮助(当我单击类别中的“添加到购物车”btn时,我应该能够添加产品,而无需转到产品页面。) PHP:Category.tpl <?php if ($product['options']) { ?> <hr> <h3><?

我已将产品选项添加到类别页面。工作正常,但当我从分类页面点击“添加到购物车”按钮时,它总是将我重定向到产品页面。所以我改变了“添加到购物车”按钮的行为,现在它调用了我的新Javascript函数。但看起来我的脚本或逻辑有问题。需要这方面的帮助(当我单击类别中的“添加到购物车”btn时,我应该能够添加产品,而无需转到产品页面。)

PHP:Category.tpl

<?php if ($product['options']) { ?>
<hr>
<h3><?php echo $text_option; ?></h3>
<?php foreach ($product['options'] as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
  <select name="option[<?php echo $option['product_option_id']; ?>]" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control">
    <option value=""><?php echo $text_select; ?></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>
</div>
<?php } ?>
<?php } ?>
<?php } ?>    

<button type="button" id="button-cart" onclick="" data-loading-text="<?php echo "Loading" ?>" class="btn btn-primary btn-lg btn-block"><?php echo $button_cart; ?></button>

我遗漏了什么吗?

那么您是否只有一个显示选项的选择框?在该页面中,OP在结束时说:“我仍在使用javascript,因此我可以将产品添加到购物车中,并提供数量和选项,而无需进入产品页面。”因此,停止重定向没有包含在该教程中。我有一个网站,在产品上有很多选择,它必须重定向您,以确定您为产品选择了哪些选项。如果您有PHP代码来显示类别页面上的选项,您需要更新“添加到购物车”click eventCategory页面上的javascript:我需要一个“添加到购物车”click event的javascript,它可以将我的产品以及所选选项添加到购物车,而不会将我重定向到产品页面。您尝试了什么?此网站不适用于代码请求,因此您需要显示您已尝试的内容,以便获得帮助。您可以从查看
category.tpl
文件和
product.tpl
文件开始,这两个文件都以不同的方式处理添加到购物车,您希望尝试将脚本从产品模板添加到类别并引用所选选项。我有PHP代码在类别页面上显示选项。category.tpl调用的方法与category.tpl中的product.tpl调用的方法相同。我已将“option”设置为控件的名称。请参阅下文。那么,您是否只有一个显示选项的选择框?在该页面中,OP在结束时说:“我仍在使用javascript,因此我可以将产品添加到购物车中,并提供数量和选项,而无需进入产品页面。”因此,停止重定向没有包含在该教程中。我有一个网站,在产品上有很多选择,它必须重定向您,以确定您为产品选择了哪些选项。如果您有PHP代码来显示类别页面上的选项,您需要更新“添加到购物车”click eventCategory页面上的javascript:我需要一个“添加到购物车”click event的javascript,它可以将我的产品以及所选选项添加到购物车,而不会将我重定向到产品页面。您尝试了什么?此网站不适用于代码请求,因此您需要显示您已尝试的内容,以便获得帮助。您可以从查看
category.tpl
文件和
product.tpl
文件开始,这两个文件都以不同的方式处理添加到购物车,您希望尝试将脚本从产品模板添加到类别并引用所选选项。我有PHP代码在类别页面上显示选项。category.tpl调用的方法与category.tpl中的product.tpl调用的方法相同。我已将“option”设置为控件的名称。请参阅下文。
<script type="text/javascript">
$('#button-cart').on('click', function() {
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
        dataType: 'json',
        beforeSend: function() {
            $('#button-cart').button('loading');
        },
        complete: function() {
            $('#button-cart').button('reset');
        },
        success: function(json) {
            $('.alert, .text-danger').remove();
            $('.form-group').removeClass('has-error');

            if (json['error']) {
                if (json['error']['input-option']) {
                    for (i in json['error']['input-option']) {
                        var element = $('#input-option' + i.replace('_', '-'));

                        if (element.parent().hasClass('input-group')) {
                            element.parent().after('<div class="text-danger">' + json['error']['input-option'][i] + '</div>');
                        } else {
                            element.after('<div class="text-danger">' + json['error']['input-option'][i] + '</div>');
                        }
                    }
                }

                if (json['error']['recurring']) {
                    $('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
                }

                // Highlight any found errors
                $('.text-danger').parent().addClass('has-error');
            }

            if (json['success']) {
                $('.breadcrumb').after('<div class="alert alert-success">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

                $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');

                $('html, body').animate({ scrollTop: 0 }, 'slow');

                $('#cart > ul').load('index.php?route=common/cart/info ul li');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
</script>
$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,
            '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'],
    'thumb'       => $image,
    'name'        => $result['name'],
    'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
    'price'       => $price,
    'special'     => $special,
    'tax'         => $tax,
    'options'     => $options,
    'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
    'text_minimum' => sprintf($this->language->get('text_minimum'), $result['minimum']),
    'rating'      => $result['rating'],
    'href'        => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
);