5

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?

3 Answers 3

10

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
4
  • 1
    add the above code in both list mode and grid mode and also don't forget to add style for both "add to cart" button and "qty" input box.
    – SIBHI S
    Commented May 27, 2014 at 15:52
  • Great answer but $this->getMinimalQty($_product) returns NULL in my case. Global minimal quantity is set to 1. Why is that?
    – Alan
    Commented Feb 23, 2015 at 10:26
  • Did you reindex data in admin? It is necessary to update your changes.
    – SIBHI S
    Commented Feb 23, 2015 at 10:48
  • I've saved the data so they are available in front-end. I can get it with $productQty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId()); $minSaleQty = $productQty->getMinSaleQty(); but I'd prefer to get it directly from the $_product object.
    – Alan
    Commented Feb 23, 2015 at 10:52
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.

1
  • A nice way to do this is adding a new parameter to getAddToCartUrl() function: $additionalUrlParameters = array('qty' => 2); $this->getAddToCartUrl($_product, $additionalUrlParameters);
    – joseantgv
    Commented Jun 1, 2016 at 17:02
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.