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

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

July 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento | Use quantity increments and ajax add to cart on category page and product view

Magento: Optimizing Quantity Increments and Ajax Add-to-Cart Functionality on Category and Product View Pages

As an experienced Magento developer, I often encounter challenges when configuring quantity increments and integrating them with the add-to-cart functionality using AJAX. In this blog post, we will explore a common scenario where the desired quantity is not being added to the cart when using increment/decrement buttons.

The Scenario Explained

You’re using an AJAX add-to-cart extension to enable seamless one-page checkout. Additionally, you’ve modified the list.phtml file to include quantity increment functionality with ”+” and ”-” buttons (source: Magento Quantity Increments JQuery Edition).

The Core Problem

There are two primary observations contributing to this issue:

  1. Quantity not retained when using increment/decrement buttons: When you click the ”+” button to increase the quantity, the value in the input field changes correctly. However, clicking the “Add to Cart” button only adds 1 product to the cart, regardless of how many times you clicked the ”+” button.

  2. Manual entry works perfectly: If you manually type the desired quantity (e.g., 5) into the input box, the add-to-cart functionality refreshes with the correct number of products in the cart.

The Code Analysis

Let’s examine the relevant code snippets to understand what might be causing this issue:

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()
{
    // ...
});

jQuery(".minus").click(function()
{
    // ...
});

This JavaScript code is responsible for appending the ”+” and ”-” buttons to the “Quantity” field and handling their click events. It successfully updates the input value, but the updated quantity isn’t passed correctly to the add-to-cart functionality.

The Solution: Modifying the Add-to-Cart Code

To integrate the quantity increment functionality with the AJAX add-to-cart button, you need to modify the existing code in list.phtml (source: Add to cart using AJAX and Magento):

The original code that needs replacement 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: ?>

Replace the above code with this updated block:

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

This modified code includes a JavaScript function setQty that retrieves the current quantity from the input field and updates the “Add to Cart” button’s onclick attribute with the desired URL containing the specified quantity.

The Root Cause of the Issue

The problem arises because the original add-to-cart code doesn’t dynamically update the AJAX request URL with the user-selected quantity when using increment/decrement buttons. By manually typing the value or triggering a change event on the input field, you ensure that the setQty function is called correctly.

The Conclusion: Resolving the Quantity Inconsistency

To resolve this issue and maintain consistency across different input methods (increment buttons, decrement buttons, manual entry), it’s essential to:

  1. Ensure that the JavaScript code responsible for handling increment/decrement button clicks triggers a change event on the quantity input field. This can be achieved by updating the click handlers as follows:
jQuery(".plus").click(function()
{
    var currentVal = parseInt(jQuery(this).prev(".qty").val());
    jQuery(this).prev(".qty").val(currentVal + 1).trigger('change');
});

jQuery(".minus").click(function()
{
    var currentVal = parseInt(jQuery(this).next(".qty").val());
    if (currentVal > 0)
    {
        jQuery(this).next(".qty").val(currentVal - 1).trigger('change');
    }
});
  1. Verify that the add-to-cart functionality is correctly integrated with the modified code in list.phtml.

By implementing these changes, you should be able to maintain consistent quantity values across all input methods when using Magento’s AJAX add-to-cart extension combined with quantity increment functionality.

Checkout Catalog Database Performance

Our network