简体   繁体   English

Magento | 使用数量增量并将ajax添加到类别页面和产品视图上的购物车

[英]Magento | Use quantity increments and ajax add to cart on category page and product view

Here is the scenario: 这是场景:

I'm using an ajax add to cart extention to add product to cart without refreshing page. 我正在使用ajax添加到购物车扩展来将产品添加到购物车而无需刷新页面。

And I have modified the list.phtml file to have some quantity increment function which adds a "+" and "-" plus and minus buttons inputs. 而且我已经修改了list.phtml文件,使其具有一些数量增加功能,该功能增加了“ +”和“-”加号和减号按钮输入。 (source: http://jigowatt.co.uk/blog/magento-quantity-increments-jquery-edition/ ). (来源: http//jigowatt.co.uk/blog/magento-quantity-increments-jquery-edition/ )。

The problem explained with 2 different observations: 通过两个不同的观察结果解释了该问题:

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. 1st /如果我通过单击+按钮输入来增加数量,则数量正确更改,因为我在输入框中看到值更改,但是随后我单击了添加到购物车按钮,并且仅添加了1种产品。 No matter how many times I clicked on the + button to get the quantity I wanted, the number added to cart is always 1. 无论我单击+按钮获取所需数量的次数,添加到购物车的数量始终为1。

2nd/ If I type the desired quantity number in the quantity box manually, 5 for example, no problem : cart is refreshed with 5 items. 2nd /如果我在数量框中手动输入所需的数量编号,例如5,没问题:购物车会刷新5件商品。

So basically only when clicking on the increment button + , the number of items aren't added, only one gets added. 因此,基本上,仅当单击增量按钮+时,才添加项目数,而仅添加一项。

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);
}
});

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 ) 现在,要使ajax添加到购物车按钮与list.phtml上的数量输入框一起使用,必须进行一些修改(来源: http : //forum.aheadworks.com/viewtopic.php? f=33&t=601)

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

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

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). 出于某种原因,数量在输入框中发生变化,但是此值不是单击添加到购物车后的购物车中的值(总是将1个产品添加到购物车)。

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: 我建议您在Google Chrome浏览器中打开页面,然后使用开发人员工具执行以下操作:

  1. Step through the jQuery code using the Script panel - you can then make sure that the code is setting the quantity correctly. 使用“ 脚本”面板单步执行jQuery代码-然后可以确保该代码正确设置了数量。

  2. Check that the request going via Ajax is correct. 检查通过Ajax发出的请求是否正确。 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. 您可以通过查看“ 网络”面板 ,识别Ajax调用并检查输入到控制器的qty值是否正确来完成此操作。

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. 就我个人而言,我将检查+(加号)和-(减号)按钮是否触发了setQty函数,或者至少要检查setQty函数与加号和减号按钮的功能相同。

From the code you have posted it looks like this line in the setQty function may be required in the plus & minus button code 从您发布的代码来看,似乎在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>'; 
  1. Cut the setQty function from input tag. 输入标签剪切setQty函数。
  2. paste it instead of setLocation function in Add To Cart Button tag 将其粘贴到“ 添加到购物车按钮”标签中,而不是setLocation函数
  3. Use onmousedown event Add To Cart Button tag. 使用onmousedown事件“ 添加到购物车按钮”标签。
  4. Use onmouseup event within script Totally the code looks like 在脚本中使用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. Use the following script to increment and decrement the value when click on anchor tags 单击锚标记时,使用以下脚本来递增和递减值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento 1.9-Ajax从类别页面/产品列表添加到购物车 - Magento 1.9 - Ajax Add to Cart from category page/product listing Magento允许产品添加到购物车(如果添加了其他类别的产品) - Magento allow product add to cart if another category product added Magento Ajax添加到购物车产品未找到错误 - Magento ajax add to cart product not found error Magento 2“添加到购物车”模式在类别页面(网格) - Magento 2 "add to cart" modal in category page (grid) ajax提交停留在同一页上+更新购物车中产品的数量 - ajax submit stay on same page + update quantity of product in cart 在 Shopify 商店中使用 AJAX 将所有产品变体和数量添加到购物车 - Add all product variant and quantity to the cart with AJAX in Shopify store 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page Ajax 加入购物车 WooCommerce 产品页面 - Ajax add to cart on WooCommerce product page Ajax上的JS警报已添加到购物车,以获取Woocommerce中特定产品类别的数量 - JS alert on ajax add to cart for specific product category count in Woocommerce Magento:通过外部站点的ajax请求将产品添加到购物车 - Magento: add product to cart by ajax request from external site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM