A
A
Alexander Kaplun2016-02-28 17:09:27
1C-Bitrix
Alexander Kaplun, 2016-02-28 17:09:27

How to make a "add to cart" button with Ajax in 1C Bitrix?

Good day,
There is a store , in the catalog.section there are buttons "add to cart"

<a href="/catalog/<?=$arElement["IBLOCK_SECTION_ID"]?>/?<?=$arParams["ACTION_VARIABLE"]?>=ADD2BASKET&id=<?=$OFFERS['ID']?>" class="btn btn-primary buy_button" rel="nofollow" onclick="return addToCart(this, 'list', '<?=GetMessage("CATALOG_IN_CART")?>', 'noCart');" id="catalog_add2cart_link_<?=$OFFERS['ID']?>"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span> В корзину</a>

how to make it so that when the button is clicked the page is not reloaded
They say addToCart has Ajax support but I can't find its description
I also heard that there is bx.ajax but I can't figure out how to use it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-02-28
@QuietusUA

See the implementation in the standard template. It describes the functions of adding ajax, but if you have a highly custom directory it will be difficult.
I usually redo the standard template and check that the standard button works, which is difficult to achieve, because by deleting some id, it no longer works.
Another such way

W
werba, 2021-02-25
@werba

<button style='width:100%; margin:5px auto;' class="product__btn btn btn--bg-green btn--color-white" type='submit' onclick="$.post('?action=ADD2BASKET&ajax_basket=Y&id=<?=$arItem["ID"]?>&quantity=1', function(data) {
if (data.STATUS == 'OK'){    
BX.onCustomEvent('OnBasketChange');    
}
});">

В корзину

</button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question