<script>
$( document ).ready(function() {
//Добавляем кнопки в карточки
function insertbtncard(){
//При загрузке
let tistore = setInterval(function() {
if (document.querySelector('.js-product.t-store__card_hidden')){
}else{
clearInterval(tistore)
setTimeout(function(){
$('.js-product .quantbtnwrap').remove();
$('<div class="quantbtnwrap"><div class="quantminus noactive"></div><input type="number" class="quanttxt" value="1"><div class="quantplus"></div></div>').insertAfter('a[href="#order"].t-btn');$(window).trigger('resize');
}, 1000);
}
}, 100);
};
insertbtncard();
$(document).on('click','.js-store-filter , .js-store-filter-chosen-item , .js-store-load-more-btn , .js-store-parts-switcher',function(e){ insertbtncard(); });
$(document).on('keydown','.js-store-filter',function(event){if(event.keyCode == 13) { insertbtncard()}});
var pravailable = 0; var quantincart=0;
$(document).on('click','a[href="#prodpopup"]',function(e){
setTimeout(function(){
$('.t-store__prod-popup__info .quanttxt').val(1); $('.t-store__prod-popup__info .quantminus').addClass('noactive');$('.t-store__prod-popup__info .quantplus').removeClass('noactive');
}, 100); });
//При клике на +
$(document).on('click','.quantplus',function(e){
$(this).siblings('.quantminus').removeClass('noactive');
pravailable= Number($(this).closest('.js-product').attr('data-product-inv'));
if(!pravailable) pravailable=1e4;
let currquant = Number($(this).siblings('.quanttxt').val());
if (pravailable>currquant){$(this).siblings('.quanttxt').val(currquant+1)
}else{$(this).addClass('noactive')};
});
//При клике на -
$(document).on('click','.quantminus',function(e){
$(this).siblings('.quantplus').removeClass('noactive');
let currquant = Number($(this).siblings('.quanttxt').val());
if (currquant>1){$(this).siblings('.quanttxt').val(currquant-1)
}else{$(this).addClass('noactive')};
});
//Ввод данных в поле
$(document).on('change keyup input click','input.quanttxt',function(e){
if (this.value.match(/[^0-9]/g)) {this.value = this.value.replace(/[^0-9]/g, '')};
//Предел 3 знака
if( $(this).val().length > 3) $(this).val($(this).val().substr(0, 3));
});
//Потеря фокуса
$(document).on('focusout','input.quanttxt',function(e){
if ( ($.trim($(this).val()) == '') || ($.trim($(this).val()) == 0 ) ){$(this).val(1)};
pravailable= Number($(this).closest('.js-product').attr('data-product-inv'));
let currquant = Number($(this).val());
if ( (currquant > pravailable) && (pravailable > 0) ){$(this).val(pravailable)};
});
$(document).on('click','a[href="#order"]',function(e){
var quantotcart = Number($(this).parent().find('.quanttxt').val());
var needsku= $(this).closest('.js-product').find('.js-product-sku').html();
pravailable= Number($(this).closest('.js-product').attr('data-product-inv'));
setTimeout(function(){
tcart__syncProductsObject__LStoObj();tcart__reDrawProducts();tcart__reDrawTotal();
$('.t706__product-title div:contains("'+needsku+'")').closest('.t706__product').addClass('ffposprod');
quantincart = Number($('.ffposprod .t706__product-quantity').html());
if(quantincart==pravailable){}else{
$('.ffposprod .t706__product-quantity').html(quantotcart+quantincart-1);$('.ffposprod .t706__product-quantity').trigger("click");$('.ffposprod input[name="tilda-tmp-cart-qnt"]').trigger("focusout");
};
}, 10);
});
});
</script>