Есть модуль всплывающей корзины при нажатии на кнопку купить
там как раз реализована данная функция
смотрите как там устроено
вот часть кода
function add_class() {
var p_id = $('input[name=\"product_id\"]').val();
var p_q = "$('input[name=\"quantity\"]').val()";
$('#button-cart').addClass(p_id).attr('onclick', 'cart.add(\''+p_id+'\', '+p_q+');');
$('.cart .button, .btn-group .btn, .btn-group .btn-primary, .button-group button').each(function() {
if(($(this).attr('onclick') && $(this).attr('onclick').substr(0, 9) == 'addToCart') || ($(this).attr('onclick') && $(this).attr('onclick').substr(0, 8) == 'cart.add')) {
var p_id = $(this).attr('onclick').substr(8, 14);
var p_class = p_id.match(/(\d+)/g);
$(this).addClass(''+p_class);
}
});
}
function restore_button(product_id) {
if($('.'+product_id).attr('id') == 'button-cart') {
$('.'+product_id).attr('onclick', 'cart.add(\''+ product_id +'\');').html(localStorage.getItem('pp_button')).removeClass('in_cart');
} else {
$('.'+product_id).attr('onclick', 'cart.add(\''+ product_id +'\');').html(localStorage.getItem('p_button')).removeClass('in_cart');
}
}