How to add quantity option to each product item in the category list page?

All we need is an easy explanation of the problem, so here it is.

How can I add quantity option to each product item in the category list page?

By default, you can find this in Magento list.phtml,

<?php if($_product->isSaleable()): ?>
    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
<?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

So every time you click the Add to Cart button, it is only 1 quantity to be added to the shopping cart.

I want to add this option so the customer can add the quantity they want at this stage,

<?php if(!$_product->isGrouped()): ?>
    <input type="text" name="qty" id="qty" placeholder="1" maxlength="12" value="<?php echo $_product->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty input-quantity" />
<?php endif; ?>

The first problem is that <?php echo $_product->getProductDefaultQty() * 1 ?> always shows the value of 0.

The second problem is that if I put the quantity value manually to be, say 20, I will still get 1 only for that product item that I have clicked to add to the shopping cart.

Any ideas?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

To add quantity box on Product Listing Page

You need to edit app/design/frontend/default/YourTheme/template/catalog/product/list.phtml file

Find the below given line

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

And just replace it with,

<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
 <?php if(!$_product->isGrouped()): ?>
 <label for="qty"><?php echo $this->__('Qty') ?>:</label>
 <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
 <?php endif; ?>
 <button type="button" onclick="this.form.submit()"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
 </form>

To change the default quantity

  1. Go to Admin Panel -> System -> Configuration -> Inventory-> Product Stock Options from the Menu
  2. Click on Add Minimum Qty button (next to Minimum Qty Allowed in Shopping Cart option)
  3. Choose Customer Group: ALL GROUPS as you want this to be applied to all users
  4. Enter 1(any value you want) for Minimum Qty
  5. Save Config

Method 2

You will have to update the onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>') on the button so that it reads the value from the quantity field and adds this to the add to cart url.

Either that or you could update the template so that each item is its own form and then you would not need to update the url but would need to take into account the formkey.

I would suggest that updating the url via JavaScript would be the simplest, depending on your knowledge of JavaScript. The final add to cart url should have /qty/3 on the end where 3 is the desired quantity.

Method 3

Thank you it worked I added the following:

<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
 <?php if(!$_product->isGrouped()): ?>
 <label for="qty"><?php echo $this->__('Qty') ?>:</label>
 <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
 <?php endif; ?>
 <button type="button" onclick="this.form.submit()"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
 </form>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply