Обновите переменные javascript после вызова ajax - PullRequest
0 голосов
/ 08 февраля 2019

Я пытаюсь динамически обновить уведомление после обновления корзины через ajax.Я не могу понять, как обновить все переменные, чтобы цикл вычислений мог снова запуститься.Я попытался обернуть оператор if в функцию и вызвать его снова внутри updated_wc_div, но это не сработало.

Есть предложения?

( function($) { 
  var membership_price = membership.price;
  //grab cart total from html
  var total = parseInt($('.order-total .woocommerce-Price-amount').text().replace('$',''));
  compared_price = parseInt(membership_price) - total;

  //everything runs fine on load.

  if ( total < membership_price ) {
    message = 'For <strong>$'+ compared_price +'</strong> more, gain access to unlimited downloads. <a href="/series/unlimited-membership">Become a member!</a>';
    notice = '<div class="woocommerce-info">' + message + '</div>';
    $('.woocommerce-notices-wrapper').html(notice);
  }
  //running this to know when ajax is called
  $(document.body).on('updated_wc_div',function( event ){
    $('.woocommerce-notices-wrapper').empty();
    total = parseInt($('.order-total .woocommerce-Price-amount').text().replace('$',''));
    //I believe problem is here, it doesn't update variable so it could loop again.
  });

} )(jQuery);

Ответы [ 2 ]

0 голосов
/ 08 февраля 2019

Просто вы должны поместить свою функцию в функцию ajax или сделать функцию ajax "async: false"

$.ajax({
    type: "POST",
    url: "",
    async:false,   // <========   remove asynchronous property 
    data: {},
    success: function(result) {
    }
  });
  
  
  ////////////////////////////////////////////////
  
  
  $.ajax({
    type: "POST",
    url: "", 
    data: {},
    success: function(result) {
    
    // Or put your function here
    }
  });
0 голосов
/ 08 февраля 2019

это то, как я динамически обновляю объект после вызова ajax:

function getCart(user_id) {
  $.ajax({
    type: "POST",
    url: "get_total_cart.php",
    data: {
      'user_id': <?php echo $user_id; ?>
    },
    beforeSend: function() {},
    success: function(response) {
      var user = JSON.parse(response);
      var total_cart = parseInt(user.total_cart);
      $("#total_cart").html(total_cart);
    }
  });
}

function addCart(product_id, user_id) {
  $.ajax({
    type: "POST",
    url: "add_cart.php",
    data: {
      'product_id': product_id,
      'user_id': user_id
    },
    beforeSend: function() {},
    success: function(response) {
      getCart(user_id);
    }
  });
}

$(document).ready(function() {
  $(".se-pre-con").fadeOut("slow");
  getCart(<?php echo $user_id; ?>);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="total_cart">Total Cart</div>

<input type="button" name="add_cart" id="add_cart" value="Add to Cart" onclick="addCart('<?php echo $product[id]; ?>', '<?php echo $user_id; ?>')" class="btn btn-info">

Надеюсь, это поможет.Приветствия.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...