Ajax 带有选项的Opencart.add()
我正在opencart中寻找解决方案 需要重建cart.add函数以添加具有选项值的产品 默认情况下:Ajax 带有选项的Opencart.add(),ajax,opencart,send,Ajax,Opencart,Send,我正在opencart中寻找解决方案 需要重建cart.add函数以添加具有选项值的产品 默认情况下: 'add': function(product_id, quantity) { $.ajax({ url: 'index.php?route=checkout/cart/add', type: 'post', data: 'product_id=' + product_id + '&quantity=' + (typeof(qua
'add': function(product_id, quantity) {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
dataType: 'json',
beforeSend: function() {
$('#cart > button').button('loading');
},
success: function(json) {
$('.alert, .text-danger').remove();
$('#cart > button').button('reset');
if (json['redirect']) {
location = json['redirect'];
}
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
}
});
有什么解决办法吗 您可以通过以下方式执行此操作。创建一个简单的
Javascript
对象
var productData = {'product_id' : PRODUCT_IDS.CARD, 'quantity': 1,'option':{'229':"Some option value"}};
并按如下方式发送请求。使用param
功能将对象转换为post params
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $.param(productData),
dataType: 'json',
beforeSend: function () {
},
complete: function () {
},
success: function (json) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
还有一些其他方法可以做到这一点,您可以在
/controller/checkout/cart.php
文件中看到可能的有效负载格式。您可以通过以下方法实现。创建一个简单的Javascript
对象
var productData = {'product_id' : PRODUCT_IDS.CARD, 'quantity': 1,'option':{'229':"Some option value"}};
并按如下方式发送请求。使用param
功能将对象转换为post params
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $.param(productData),
dataType: 'json',
beforeSend: function () {
},
complete: function () {
},
success: function (json) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
还有一些其他方法可以做到这一点,您可以在
/controller/checkout/cart.php
文件中看到可能的有效负载格式。我使用了此处描述的解决方案:我使用了此处描述的解决方案: