Magento的| 使用量增量和Ajax添加到购物车类网页和产品上的观点(Magento | U

2019-06-27 03:02发布

下面是这种情况:

我使用的是AJAX添加到购物车extention产品添加到购物车无需刷新页面。

我已经修改了list.phtml文件有一定的数量递增函数,它增加了一个“+”和“ - ”加号和减号按钮输入。 (来源: http://jigowatt.co.uk/blog/magento-quantity-increments-jquery-edition/ )。

问题2个不同的看法解释说:

1 /如果我通过点击+按钮输入增加数量,数量正确改变,因为我看到价值在输入框中改变,但后来我点击添加到购物车按钮,而且也只有1产品加入。 不管有多少次我点击+按钮来获得我想要的数量,增加数量,车始终为1。

第二/如果我在数量框中键入所需量数手动,5例如,没问题:购物车被刷新与5个项目。

所以基本上只对增量键+点击时,不添加物品的数量,只有一个被添加。

下面是它增加了增值功能,并增加了+和代码 - 按钮:

jQuery("div.quantity").append('<input type="button" value="+" id="add1"    class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
jQuery(".plus").click(function()
{
var currentVal = parseInt(jQuery(this).prev(".qty").val());

if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 0;

jQuery(this).prev(".qty").val(currentVal + 1);
});

jQuery(".minus").click(function()
{
var currentVal = parseInt(jQuery(this).next(".qty").val());
if (currentVal == "NaN") currentVal = 0;
if (currentVal > 0)
{
jQuery(this).next(".qty").val(currentVal - 1);
}
});

现在,有AJAX添加到与list.phtml数量输入框车按钮的工作,一些修改不得不作出(来源: http://forum.aheadworks.com/viewtopic.php?f=33&t=601 )

必须更换原来的代码是:

<!-- Find this block of code: -->
<?php if($_product->isSaleable()): ?>
<button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>

它必须与下面这段代码来替代,如上面贴的论坛链接解释说:

<!-- And replace it with this block of code: -->
<?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" onclick="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Add to Cart</span></span></button>';   
}
</script>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input type="text" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="1" onkeyup="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<span id="cart_button_<?php echo $_product->getId(); ?>"><button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></span> 
<?php else: ?>

我不知道为什么手动键入值时加入购物车的数量是唯一正确的。 我需要使用正(+)时,也添加到购物车正确的数量或 - (减号)按钮。 出于某种原因,数量在输入框中的变化,但这个值是不是一个在车后添加到购物车点击(总是1个产品加入购物车)。

是什么原因造成这个问题? 而这将是解决这个问题的解决? 我很想了解和解决这个问题,因为我一直在这整个下午的努力。 非常感谢。

Answer 1:

打算把这个作为一个评论,但需要对其进行格式化以方便查看

我建议谷歌浏览器打开该网页,然后使用开发工具做了两件事情:

  1. 通过使用jQuery代码步骤脚本面板 -那么你可以确保代码正确设置数量。

  2. 检查通过Ajax去请求是正确的。 您可以通过查看为此网络面板 ,确定Ajax调用和检查的数量值将控制器是正确的。

就个人而言,我会检查该setQty功能正在由正(+)发射和 - (减)按钮,或至少使setQty功能做同样的事情作为正和负的按钮。

从你的代码已经发布,它看起来像这条线在setQty功能可以在加和减键代码需要

document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="button" onclick="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Add to Cart</span></span></button>'; 


Answer 2:

  1. 剪切从输入标签setQty功能。
  2. 添加贴吧,而不是setLocation功能to Cart按钮标签
  3. 使用onmousedown事件事件添加到购物车按钮标签。
  4. 脚本完全的代码看起来像内使用onmouseup事件

      <?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> <input type="text" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="1" title="<?php echo $this->__('Qty') ?>" class="input-text qty" /> <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; ?> 
  5. 使用下面的脚本来增减值当点击锚标签

     <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> 


文章来源: Magento | Use quantity increments and ajax add to cart on category page and product view