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

时间:2017-08-28 12:05:26

标签: opencart categories options opencart2.x

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

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>

JS:

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

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,
            '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)
);

我错过了什么吗?

0 个答案:

没有答案