opencart更新购物车总数ajax

时间:2014-01-07 16:03:51

标签: javascript jquery html ajax opencart

我正在尝试在标题中实施购物车,以便在我将商品添加到购物车时更新其价格/商品

我的HTML:

<div id="cart">
<p class="phone">01234 567890</p>
<div class="basket">
 <p class="title"><a href="<?php echo $shopping_cart; ?>">Basket</a></p>
 <p class="items">
  <?php
  $cart_number = $this->cart->countProducts();
  if ($cart_number == 1) {
   echo "1 item";
  } else {
   echo $cart_number." items";
  }
  ?>
 </p>
 <p class="price">
  <?php echo $this->currency->format($this->cart->getTotal()); ?>
 </p>
  <script type="text/javascript">
   <!--
   $(document).ready(function () {
    $('#button-cart').bind('click', function() {
$.ajax({
     url: 'index.php?route=checkout/cart/add',
 type: 'post',
 data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
 dataType: 'json',
 success: function(json) {
  $('.success, .warning, .attention, information, .error').remove();

  if (json['error']) {
   if (json['error']['option']) {
    for (i in json['error']['option']) {
     $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
    }
   }
  } 

  if (json['success']) {
   $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
   $('.success').fadeIn('slow');
   $('.price').html(json['total']);
   $('html, body').animate({ scrollTop: 0 }, 'slow'); 
  } 
 }
    });
   });
   });
   //-->
  </script>
</div>

这是一个自定义主题,但大多数元素都是从默认主题复制的,但由于某种原因,它不会更新购物车项目/总计

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:3)

你必须在最后刷新#cart div

添加行

$('#cart').load('index.php?route=module/cart #cart > *');

$('html, body').animate({ scrollTop: 0 }, 'slow');
相关问题