S
S
symply_rabbit2019-04-20 17:54:09
JavaScript
symply_rabbit, 2019-04-20 17:54:09

Why is the value one step less passed through ajax?

Good afternoon! Please tell me what my cant be. The code is executed but with an error.

In general, there is a standard basket on the Bitrix solution. there you need to add the impossibility to issue a basket amount of less than 1000 rubles, that is, if this amount is less than 1000 ru, the inscription "basket amount is less than 1000 rubles" and if more then the checkout button. at the same time, the standard basket already has the functionality that by clicking on + and - the amount of the basket increases for the product, so I need that when I click on these pros and cons, my inscription / button also changes in accordance with the new amount. I decided to implement it using .load

Plus, minus and span where the answer changes in the standard solution is as follows

<a href="#" class="minus" data-id="50"></a>
<input name="qty" type="text" value="1" class="qty" data-max-quantity="80" data-id="50" data-ratio="1">
<a href="#" class="plus" data-id="50"></a>


<span id="allSum">1 280     руб.</span>


wrote the following script
<script type="text/javascript">


$('.plus').click(function {

    
 $("#bookbutcart").load("/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php"); // замена области в этом файле

  
});


$('.minus').click(function {

$("#bookbutcart").load("/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php"); // замена области в этом файле
            
  
});
</script>


Where in the bookbutoon_cart.php file is the code for displaying the cart and the area with which the previously displayed button / text is replaced. I am attaching the code

<?
CModule::IncludeModule("sale");

   $arBasketItems = array();
   $dbBasketItems = CSaleBasket::GetList(
                  array("NAME" => "ASC","ID" => "ASC"),
                  array("FUSER_ID" => CSaleBasket::GetBasketUserID(), "LID" => SITE_ID, "ORDER_ID" => "NULL"),
                  false,
                  false,
                  array("ID","MODULE","PRODUCT_ID","QUANTITY","CAN_BUY","PRICE"));
   while ($arItems=$dbBasketItems->Fetch())
   {
      $arItems=CSaleBasket::GetByID($arItems["ID"]);
      $arBasketItems[]=$arItems;   
      $cart_num+=$arItems['QUANTITY'];
      $cart_sum+=$arItems['PRICE']*$arItems['QUANTITY'];
   }
   if (empty($cart_num))
      $cart_num="0";
   if (empty($cart_sum))
      $cart_sum="0";
   ?>
<?/*
   В вашей корзине  <?=$cart_num?> товаров.На сумму <?=$cart_sum?> рублей
 */?>  



<div id="bookbutcart">
<?if ($cart_sum<1000) {?>
Сумма заказа должна быть не менее 1000 руб
<?}
else {?>


    <a href="<?=SITE_DIR?>personal/cart/order/" id="newOrder" class="selected"><img src="<?=SITE_TEMPLATE_PATH?>/images/order.png">Оформить заказ</a>
<?}?>
</div>


The code is working and everything basically works, BUT !!! the value one click less is returned. to make it clearer I will attach a screen
5cbb315555988619693463.png
to fix this problem I tried to bind to the settimeout function of this kind
function func() {
  alert( 'Привет' );
}
setTimeout(func, 1000);


I also tried to transfer the value through Ajax to the file where the load is, the code is below
<script type="text/javascript">
$('.plus').click(function (){
    
 $.ajax({
    type: "POST",
    url: "/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php",
                 data: ({summ:$('#allSum').text()}),
cache: false,
success: function(data) {
    $("bookbutcart").load("/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php"); // замена области в этом файле
            },
  });
  
});

$('.minus').click(function (){
     $.ajax({
    type: "POST",
    url: "/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php",
                 data: ({summ:$('#allSum').text()}),
cache: false,
success: function(data) {
    $("bookbutcart").load("/bitrix/templates/dresscode/components/dresscode/sale.basket.basket/standartOrder/bookbutoon_cart.php"); // замена области в этом файле
            },
  });
  
});
</script>


but the value is transmitted again, one step less ... Gentlemen, experts, help, how could this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
symply_rabbit, 2019-04-22
@symply_rabbit

The problem was that a bunch of functions were running on the page and my code was going in the wrong order.
I set a settime out of 3 seconds, so it began to be launched by the last function on the page and then everything worked out. and besides, I found where the standard click is displayed and added my code (together with settime out) there

setTimeout(function() { $("#bookbutcart").load("/include/bookbutoon_cart.php"); // замена области в этом файле
                       }, 3000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question