编程语言
首页 > 编程语言> > javascript-Opencart向opencart添加产品选项

javascript-Opencart向opencart添加产品选项

作者:互联网

除产品选项外,我的购物车似乎正在运行.当我单击添加购物车按钮时,该项目被添加,但是没有添加任何选项.我真的不明白为什么会这样,因为我使用option_id和option_value_id将函数作为函数提交的选项作为数组提交了

单击按钮时调用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

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

        }  

这是options数组的var_dump

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

解决方法:

您在其中传递了$key =>的第一个选项($key => value); 13应该是有效密钥

在Option($key => $Value)数组中,其中$key代表product_option_value表的product_option_id_id,因此$value代表product_option_value表的Product_option_value_id,因此它们应该是有效的,当您将选项分配给产品而不是静态id时会动态分配.

**第二**只需使用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'); 
            }   
        }
    });
});

标签:opencart,cart,javascript,php,options
来源: https://codeday.me/bug/20191013/1906898.html