这里是场景:Magento |使用数量增量和阿贾克斯添加到购物车的类别页面和产品视图
我正在使用ajax添加到购物车扩展添加产品到购物车没有刷新页面。
而且我修改了list.phtml文件以增加一个“+”和“ - ”加号和减号按钮输入的数量增量功能。 (来源:http://jigowatt.co.uk/blog/magento-quantity-increments-jquery-edition/)。
第一/如果我通过点击+按钮输入增加数量,量正确地更改作为我看到的值在输入框中发生变化,但:
问题与2个不同的观察结果说明然后我点击添加到购物车按钮,并且只添加了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件商品添加到购物车),此值不是购物车中的数量。
是什么导致了这个问题?解决这个问题的解决方案是什么?我很想理解并解决这个问题,因为我整个下午都在尝试。非常感谢。
嘿非常感谢您花时间回复我。我离开了一段时间,只是检查了你的答案。对于setQty函数你必须是正确的:当点击加号或减号按钮时,它被调用。我尝试直接追加它:jQuery(“div.quantity”)。append(' getId();?>,\'<? php echo $ this-> getAddToCartUrl($ _ product)?> \');“value =”“id =”minus1“class =”minus“/>')但没有奏效。 – mlclm 2012-08-28 20:36:39