Magento的|使用量增加和AJAX添加到购物车类别页面和产品视图购物车、视图、使用量、类别

2023-09-11 01:26:26 作者:小不正经

下面是该方案:

我使用AJAX添加到购物车延伸到产品添加到购物车无刷新页面。

I'm using an ajax add to cart extention to add product to cart without refreshing page.

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

And I have modified the list.phtml file to have some quantity increment function which adds a "+" and "-" plus and minus buttons inputs. (source: http://jigowatt.co.uk/blog/magento-quantity-increments-jquery-edition/).

的问题有2个不同的观测解释:的

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

1st/ If I increase the quantity by clicking on the + button input, quantity changes correctly as I see the value changing in the input box , but then I click on add to cart button, and there's only 1 product added. No matter how many times I clicked on the + button to get the quantity I wanted, the number added to cart is always 1.

第二/ 如果我手动键入数量框中输入所需的量数,5为例,没有任何问题:车被刷新5项

2nd/ If I type the desired quantity number in the quantity box manually, 5 for example, no problem : cart is refreshed with 5 items.

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

So basically only when clicking on the increment button + , the number of items aren't added, only one gets added.

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

Here is the code which adds the increment function and adds the + and - buttons:

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 )

Now, to have the ajax add to cart button work with the quantity input box on list.phtml, some modification had to be made (source: http://forum.aheadworks.com/viewtopic.php?f=33&t=601)

原来的code必须更换为:

The original code which must be replaced is :

<!-- 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: ?>

必须与此$ C $低于C取代,如上面贴的论坛链接解释:

It must be replaced with this code below, as explained on the forum link posted above:

<!-- 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产品加入购物车)。

I don't know why the quantity added to cart is only correct when typing the value manually. I would need the correct quantity added to cart also when using the + (plus) or - (minus) buttons . For some reason the quantity changes in the input box, but this value is not the one in the cart after the add to cart is clicked (always 1 product added to cart).

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

What is causing this problem ? And what would be the solution to resolve this ? I would love to understand and fix this as I've been trying this whole afternoon. Many thanks.

推荐答案

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

Was going to put this in as a comment, but need to format it for easier viewing

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

I would recommend opening the page in Google Chrome and then using the developer tools to do a couple of things:

通过jQuery的code。使用脚本面板步骤 - 那么你就可以确保在code是正确的设置数量。

Step through the jQuery code using the Script panel - you can then make sure that the code is setting the quantity correctly.

检查请求是否通过Ajax会是正确的。您可以通过查看网络面板做到这一点,确定Ajax调用并检查的数量值将控制器是正确的。

Check that the request going via Ajax is correct. You can do this by looking at the Network panel, identifying the Ajax call and checking that the qty value going to the controller is correct.

就个人而言,我会检查该setQty功能被解雇+(加号)及 - (减号)按钮,或者至少该setQty功能做同样的事情作为正和放大器;减号按钮。

Personally, I'd be checking that the setQty function is being fired by the + (plus) & - (minus) buttons, or at least that the setQty function is doing the same thing as the plus & minus buttons are.

从code您已发布它看起来像这条线在setQty功能可能需要在加和放大器;减号按钮code

From the code you have posted it looks like this line in the setQty function may be required in the plus & minus button code

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