var sum = 0,
selectPriceForm;
function insert() {
//Считываем файл form.json
$.getJSON("/forms.json", insertForms);
}
function insertForms(data) {
//Вывод на страницу
console.log(data);
var out='';
for (var key in data) {
out += '<option data-title="'+data[key].name+'" data-price="'+data[key].cost+'">'+data[key].name+'</option>';
}
$('#select-form').append(out);
}
function insertToCart(data) {
//Вывод на страницу
var selectTitleForm = $('#select-form option').filter(':selected').data('title');
console.log(selectTitleForm);
var out='';
for (var key in data) {
out += '<div class="popup-good__title">';
out += selectPriceForm
out += '</div>'
}
$('.popup-goods').html(out);
}
function changeForm() {
selectPriceForm = $('#select-form option').filter(':selected').data('price');
$('.priceFormed').text(selectPriceForm + ' руб.');
recalc();
}
$('#select-form').change(function() {
changeForm();
});
$('.calculator-icons__button').click(function() {
insertToCart();
});
<div class="dropdown">
<select id="select-stamp" class="calculator-steps__choise">
<option selected data-price="0">Выбрать другую оснастку</option>
</select>
</div>
<button class="calculator-icons__button">Добавить в корзину</button>
var cart = {};
function CartInsert() {
//добавляем товар в корзину
var id = $('#target').attr('data-id');
//console.log(id);
if (cart[id]==undefined) {
cart[id] = 1; //если нет товара = 1
}
else {
cart[id]++; //если товар есть +1
}
CalcInCart();
}
function loadCart() {
// проверка на наличие записи cart в localStorage
if (localStorage.getItem('cart')) {
// расшифровка и запись переменной
cart = JSON.parse(localStorage.getItem('cart'));
CalcInCart();
}
else {
$('.popup-goods').html('Корзина пуста!');
}
}
function CalcInCart() {
$.getJSON('/mainstamp/wp-content/themes/stamp-theme/prints.json', function (data) {
var out = '';
for (var key in cart) {
out += '<button data-id="'+key+'" class="del-goods">x</button>';
out += '<div class="popup-good">';
out += '<div class="popup-good__img">';
out += '<img src="/mainstamp/wp-content/themes/stamp-theme/'+data[key].img+'">';
out += '</div>';
out += '<div class="popup-good__title">';
out += data[key].name;
out += '</div>';
out += '<div class="popup-good__color">';
out += '<img src="/mainstamp/wp-content/themes/stamp-theme/'+data[key].color+'">';
out += '</div>';
out += '<div class="popup-good__stamp">';
out += '</div>';
out += '<div class="popup-good__technology">';
out += data[key].technology;
out += '</div>';
out += '<div class="popup-good__time">';
out += data[key].time
out += '</div>';
out += '<div class="poputp-good__score">';
out += '<button data-id="'+key+'" class="minus-goods">-</button>'
out += cart[key];
out += '<button data-id="'+key+'" class="plus-goods">+</button>'
out += '</div>';
out += '<div class="popup-good__price">';
out += cart[key]*sum;
out += '</div>';
out += '</div>';
}
$('.popup-goods').html(out);
});
}
$(document).ready(function() {
loadCart();
});
$('#select-stamp').change(function() {
changePrice();
$('#target').attr('data-id', $('#select-stamp option').filter(':selected').data('id'));
});
$('#target').on('click', function() {
CartInsert();
});