简体   繁体   English

Opencart在opencart中添加产品选项

[英]Opencart add product options to opencart

My cart appears to be working except the product options. 除产品选项外,我的购物车似乎正在运行。 When I click add cart button then the item gets added, but no options are added with it. 当我单击添加购物车按钮时,该项目被添加,但是没有添加任何选项。 I really don't understand why this is happening as I have submitted the options as an array just as the function requires, using option_id and option_value_id 我真的不明白为什么会这样,因为我使用option_idoption_value_id将函数作为函数提交的选项作为数组提交了

JavaScript called when button is clicked 单击按钮时调用JavaScript

$('#button-cart').on('click', function() {
    var model_select = $('#model option:selected').val();

    alert("working");
    $.ajax({
        url: '<?php echo $action?>',
        type: 'post',
        data: {'option' : $('#network option:selected').val(),'product_id': model_select, 'ajax':'1'},
        success: function(json) {
            $('.success, .warning, .attention, information, .error').remove();

            if (json['error']) {
                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                    }
                }
            } 

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

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

PHP PHP

if (isset($_REQUEST['product_id']) && isset($_REQUEST['option'])) {
            $product_id = $_REQUEST['product_id'];
            $option=array("13" => (int)$_REQUEST['option']);
            var_dump($option);
            $this->cart->add($product_id,$quantity=1,$option);
            print_r($this->session->data['cart']);

        }  

Here is the var_dump of the options array 这是options数组的var_dump

array(1) { [13]=> int(60) }

First Option($key=>value) where you have passed $key => 13 which should be valid key 您通过$ key => 13的第一个选项($ key => value)应该是有效的密钥

in array of Option($key=>$Value) where $key represents product_option_id and $value represents Product_option_value_id of product_option_value table so these should be valid which is assigned dynamically when you assign option to product rather than static id. Option($key=>$Value)数组中,其中$key代表product_option_id表的product_option_id$value代表product_option_value表的Product_option_value_id ,因此它们应该是有效的,当您将选项分配给产品而不是静态id时会动态分配。

**Second** Just use the default method of opencart, this will handle other input type as well **第二**只需使用opencart的默认方法,这也会处理其他输入类型

$('#button-cart').bind('click', function() {
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
        dataType: 'json',
        success: function(json) {
            $('.success, .warning, .attention, information, .error').remove();

            if (json['error']) {
                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                    }
                }

                if (json['error']['profile']) {
                    $('select[name="profile_id"]').after('<span class="error">' + json['error']['profile'] + '</span>');
                }
            } 

            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM