AJAX - 添加到购物车不会更新Magento中list.phtml(产品列表页面)中的购物车

时间:2016-02-01 10:32:41

标签: php magento

enter image description here

上面我添加了产品列表页面的图像。

我的问题是当我在购物车中添加5件商品时,它正在正确添加。但是,当我将其增加到6并再次“将相同的项目添加到购物车”时,该项目的总数量在购物车中变为11。但我的要求是不要将(6 + 5 = 11)项添加到购物车,它应该变为6。

我的代码如下:

<?php if($_product->isSaleable()): ?>  

<script type="text/javascript">
 function setQty(id, url) {
 var qty = document.getElementById('qty_' + id).value;
 document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="button" onmouseup="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Add to Cart</span></span></button>';   
 }
</script>   

<label for="qty"><?php echo $this->__('Qty:') ?></label>

<a class="decrement_qty" href="javascript:void(0)">  &nbsp - &nbsp</a>
<?php 

$_quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $_quote ? $_quote->getItemByProduct($_product) : false;
$qty = $_item ? $_item->getQty() : 1;
$style = ($qty > 1) ? "background-color: #84c1e0;" :  "background-color:white";
?>
<input type="text" 
 name="qty_<?php echo $_product->getId(); ?>" 
 id="qty_<?php echo $_product->getId(); ?>" 
 maxlength="12" value="<?php echo $qty;?>" 
 title="<?php echo $this->__('Qty') ?>" class="input-text qty" style="       <?php  echo $style;?>"/>

<a class="increment_qty" href="javascript:void(0)"> &nbsp + &nbsp</a> 

<span id="cart_button_<?php echo $_product->getId(); ?>">
<button type="button" 
        class="button"
        onmousedown="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
</span>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

jquery code for increment and dcrement the quantity is below.

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.increment_qty').click(function() {
  var oldVal = jQuery(this).parent().find("input").val();
    if ( parseFloat(oldVal) >= 1 ) {
    var newVal = parseFloat(oldVal) + 1;
    jQuery(this).parent().find("input").val(newVal);
  }
 });

 jQuery('.decrement_qty').click(function() {
  var oldVal = jQuery(this).parent().find("input").val();
  if ( parseFloat(oldVal) >= 2 ) {
    var newVal = parseFloat(oldVal) - 1;
    jQuery(this).parent().find("input").val(newVal);
  }
  });
  });
  </script>    

注意:我也在使用AJAX添加到购物车扩展程序。

0 个答案:

没有答案
相关问题