Opencart为opencart添加产品选项 [英] Opencart add product options to opencart
问题描述
我的购物车似乎正在运行,但产品选项除外。当我单击添加购物车按钮时,该项目将被添加,但不会添加任何选项。我真的不明白为什么会发生这种情况,因为我已经按照函数的要求将选项提交为数组,使用 option_id
和 option_value_id
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
点击按钮时调用的JavaScript
JavaScript called when button is clicked
$('#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 选项数组的code>
array(1) { [13]=> int(60) }
推荐答案
首先选项($ key => value)你已经传递了$ key => 13,这应该是选项数组中的有效密钥
First Option($key=>value) where you have passed $key => 13 which should be valid key
$ key => $ Value)其中 $ key
代表 product_option_id
和 $ value
代表 Product_option_value_id
product_option_value
表,所以这些应该是有效的,这是在为产品而非静态ID分配选项时动态分配。
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.
**第二** 只需使用opencart的默认方法,这将是处理其他输入类型
**Second** Just use the default method of opencart, this will handle other input type as well
$('#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为opencart添加产品选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!