Magento: Show Quantity Box in Category View

Thanks to nebil and pastanislas over on the Magento board for this one.

In developing a template for a Wholesale Site, I want to be able to have a Quantity Box in each category view. Typically, a wholesale customer is placing an order for large numbers of items. Forcing him to drill down into an individual product is exactly perfectly not what we want.

Rather, we want our customer to be able to specify quanities and click the Add to Cart button, and keep moving right down the page.

The gang over at the Magento board had this solved for me in no time!

In my theme’s /template/catalog/product/list.phtml file, I changed the <button> line near line 108 from this:

107
108
<?php if($_product->isSaleable()): ?>
<button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>

to this:

108
109
110
111
112
113
114
115
116
117
118
119
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId(); ?>">
109 <input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />
<button class=form-button" onclick="productAddToCartForm_<?php echo $_product->getId(); ?>.submit()"><span><?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript">
     var productAddToCartForm_<?php echo $_product->getId(); ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId(); ?>');
     productAddToCartForm_<?php echo $_product->getId(); ?>.submit = function(){
     if (this.validator.validate()) {
             this.form.submit();
         }
     }.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);
</script>

NOTE!!! This is in TWO places! You’ll have to make a similar change around line 62 as well. A search on isSaleable() should do the trick.

The only real change from the code found in the Magento thread is that I made the button class “form-button” it was just plain “button” in the original.