Битрикс. Добавляем количество товара в корзину через параметр URL
0
12.04.2020 07:56 12.04.2020 07:56:43
Для добавления в корзину товара и количества через параметр URL я использую следующий скрипт JS
<script type="text/javascript">
$(document).ready(function() {
$('input.quantity').change(function() {
let obAddToCartLink = document.getElementById('addtoCart');
console.log(obAddToCartLink);
let link_str = String(obAddToCartLink);
replace_str = link_str.replace(/(quantity=)[0-9]+/ig, '$1'+$(this).val());
obAddToCartLink.setAttribute('href', replace_str);
});
$('input.quantity').keypress(function() {
$(this).trigger('change');
});
$('a.price-card__quantyty-plus, a.price-card__quantyty-minus').click(function(e){
e.preventDefault();
e.stopPropagation();
var oThisQuntityInput = $('input.quantity:first', $(this).parent().parent());
var iThisQuantity = parseInt(oThisQuntityInput.val());
var iSubtrahend = 1;
if ($(this).hasClass("price-card__quantyty-plus"))
{
if (iThisQuantity < 2)
{
return false;
}
iSubtrahend = iSubtrahend * (-1);
}
var iThisQuantityNew = iThisQuantity + iSubtrahend;
oThisQuntityInput.val(iThisQuantityNew);
oThisQuntityInput.trigger('change');
});
});
</script>
И HTML код:
<a class="price-card__quantyty-plus" href="#">+</a>
<input type="text" class="quantity price-card__quantyty-count" name="QUANTITY_1" value="1" id="QUANTITY_1">
<a class="price-card__quantyty-minus" href="#">-</a>
<a href="<?= $arResult["ADD_URL"] . '&quantity=1' ?>" id="addtoCart" class="addtoCart btn-basket">В корзину</a>
Суть JS скрипта в том что мы меняем значение в URL quantity=1 на значение которое передается из input с классом quantity.
12.04.2020 07:56 12.04.2020 07:56:43
шаблон: