除産品選項外,我的購物車似乎正在運作.當我單擊添加購物車按鈕時,該項目被添加,但是沒有添加任何選項.我真的不明白為什麼會這樣,因為我使用option_id和option_value_id将函數作為函數送出的選項作為數組送出了
$('#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('' + json['error']['option'][i] + '');
}
}
}
if (json['success']) {
$('#notification').html('
' + json['success'] + '
![](/image/catalog/view/theme/default/image/close.png)
');
$('.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('' + json['error']['option'][i] + '');
}
}
if (json['error']['profile']) {
$('select[name="profile_id"]').after('' + json['error']['profile'] + '');
}
}
if (json['success']) {
$('#notification').html('
' + json['success'] + '
![](/image/catalog/view/theme/default/image/close.png)
');
$('.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