P
P
Pavel Sidorov2020-07-16 19:45:32
1C-Bitrix
Pavel Sidorov, 2020-07-16 19:45:32

Why does the data on the basket either come or not?

Good,
I'm trying to get data on the quantity in the basket. Information that is, then no, but the goods regularly get into the basket.
What am I doing wrong? I think about the cache, but it should not be on the idea.

api for the current quantity in the cart

use Bitrix\Main\Loader;
Loader::includeModule('sale');
Loader::includeModule('catalog');
$cntBasketItems2 = CSaleBasket::GetList(
  array(),
  array(
    "FUSER_ID" => CSaleBasket::GetBasketUserID(),
    "LID" => "s5",
    "ORDER_ID" => "NULL"
  ),
  array()
);


html cart
<div id="" class="bx-basket bx-opener" style="display: contents;">
    <a href="/dverimall/personal/cart/" class="basket-button">
      <i class="basket-button__icon">
        <span class="basket-button__count" style="" id="tt"><?echo $cntBasketItems2;?></span>
      </i>
    </a>
</div>


js getting data from handler
$.get('/bitrix/templates/dveri/ajax/count_basket.php').done(function(data){
                data = $.parseJSON(data);
                $('#tt').text(data);
              });


handler for getting updated data from the cart
use Bitrix\Main\Loader;
Loader::includeModule('sale');
Loader::includeModule('catalog');

$cntBasketItems = CSaleBasket::GetList(
  array(),
  array(
    "FUSER_ID" => CSaleBasket::GetBasketUserID(),
    "LID" => "s5",
    "ORDER_ID" => "NULL"
  ),
  array()
);

if ($cntBasketItems === 0) {
  // Если в корзине нет товаров
}

echo json_encode($cntBasketItems);


js works as soon as I put something in the basket

upd
scheme is like this. I put the goods No. 1 - the data comes correct. I go to the second product No. 2, I put it - the data comes incorrect. after refreshing the page, the quantity is updated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2020-07-17
@pavel__sidorov

Landmark - you have several ajax-s triggered. js is asynchronous. And even though you think that the first time works correctly for you, and the second one doesn’t work correctly - it’s not so, in one of the n-tsat attempts the first time will also work incorrectly
1st ajax

$.ajax({
                url: '/bitrix/templates/dveri/ajax/basket.php'

2nd ajax
$.get('/bitrix/templates/dveri/ajax/count_basket.php').done(function(data){

By default, the $.ajax parameter is async = true, so you have 3 options for solving the problem
1. Change the $.get function call to $.ajax and add the parameter in both calls
$.ajax({
//.............
async: false,
//.............

2. Move the $.get function call to success $.ajax({
3. Do it via async await (equivalent if you write promises, but this is not cool), it looks like this
//.............
 $("#send").on("click", async function () {
//.............
await  $.ajax({
//.............
await $.get('/bitrix/templates/dveri/ajax/count_basket.php').done(function(data){
//.............

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question