我的手推车似乎在工作,除了产品的选择。当我单击add cart按钮时,项目将被添加,但没有添加任何选项。我真的不明白为什么会发生这种情况,因为我按照函数的要求,使用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']);
} 下面是选项数组的var_dump
array(1) { [13]=> int(60) }发布于 2014-06-11 10:58:14
First Option($key=>value),其中您通过了$key => 13,它应该是有效的密钥。
在Option($key=>$Value)数组中,$key表示product_option_id,$value表示product_option_value表的Product_option_value_id,因此这些表应该是有效的,在将选项分配给product而不是静态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');
}
}
});
});https://stackoverflow.com/questions/24138334
复制相似问题