Page 1 of 1
Opencart Add to Cart button doent work
Posted: Fri Sep 15, 2017 2:24 am
by Shnabel
I have problem on my eshop which is made on Opencart platform.
I have just one product on website and this product has got a lot of variants. All variants are made by forms, checkboxs.. etc.
But when I add unique ID to each div of these forms, Add to Cart button stops working with no error message.
I have tried to Add unique ID with javascript:
Code: Select all
$('#product div:not(:last)').attr('id', function(i) {
return 'form'+(i+1);
});
And in product.tpl by php:
Code: Select all
<div id="form<?php echo $i++; ?>" class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
Can someone help me?
Re: Opencart Add to Cart button doent work
Posted: Fri Sep 15, 2017 3:38 pm
by thekrotek
And how exactly clicking on button works in your code? It can be absolutely different depending on template.
Re: Opencart Add to Cart button doent work
Posted: Fri Sep 15, 2017 11:19 pm
by Shnabel
This i script for my button, and I am using default template, just with custom styles
Code: Select all
<!--
$('#button-cart').on('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
dataType: 'json',
beforeSend: function() {
$('#button-cart').button('loading');
},
complete: function() {
$('#button-cart').button('reset');
},
success: function(json) {
$('.alert, .text-danger').remove();
$('.form-group').removeClass('has-error');
if (json['error']) {
if (json['error']['option']) {
for (i in json['error']['option']) {
var element = $('#input-option' + i.replace('_', '-'));
if (element.parent().hasClass('input-group')) {
element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
} else {
element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
}
}
}
if (json['error']['recurring']) {
$('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
}
// Highlight any found errors
$('.text-danger').parent().addClass('has-error');
}
if (json['success']) {
$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
//-->