Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<div class="quantity">
<input type="number" value="1" min="1" class="qu">
</div>
<a href="javascript:void(0);" id="<?=$arResult[ID];?>" class="buy btn btn--white"><span>Купить</span></a>
<script>
$(document).ready(function(){
$('<div class="quantity-nav"><div class="quantity-button quantity-up">+</div><div class="quantity-button quantity-down">-</div></div>').insertAfter('.quantity input');
$('.quantity').each(function() {
var spinner = jQuery(this),
input = spinner.find('input[type="number"]'),
btnUp = spinner.find('.quantity-up'),
btnDown = spinner.find('.quantity-down'),
min = input.attr('min'),
max = input.attr('max');
btnUp.click(function() {
var oldValue = parseFloat(input.val());
if (oldValue >= max) {
var newVal = oldValue;
} else {
var newVal = oldValue + 1;
}
spinner.find("input").val(newVal);
spinner.find("input").trigger("change");
});
btnDown.click(function() {
var oldValue = parseFloat(input.val());
if (oldValue <= min) {
var newVal = oldValue;
} else {
var newVal = oldValue - 1;
}
spinner.find("input").val(newVal);
spinner.find("input").trigger("change");
});
});
$(".buy").click(function(){
var id = $(this).attr('id');
var q = parseInt($(".qu").val());
var form_data = new FormData();
form_data.append('id', id);
form_data.append('q', q);
$.ajax({
url: '/api/add-basket.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
$(".ans").html("Товар добавлен в корзину");
$(".ans").fadeIn(1000);
setTimeout($(".ans").fadeOut(600), 5000);
var inC = parseInt($(".top-cart span").text());
var updute = inC+q;
$(".top-cart span").text(updute);
}
});
event.preventDefault(); // Полная остановка происходящего
});
});
</script>
<? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include.php");?>
<?
if (CModule::IncludeModule("sale") && CModule::IncludeModule("catalog"))
{
if (isset($_REQUEST['id'])&&isset($_REQUEST['q'])) {
$PRODUCT_ID = intval($_REQUEST['id']);
$QUANTITY = intval($_REQUEST['q']);
Add2BasketByProductID(
$PRODUCT_ID,
$QUANTITY
);
//LocalRedirect("/personal/basket.php"); //путь в Вашей корзине
}
else {
echo "Нет параметров ";
}
}
else {
echo "Не подключены модули";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.