Thursday 18 April 2013

Magento: Show Quantity Box in Category View

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.

5 comments:

  1. Hey thank you for the post, it has help me a lot, Now I'm trying to pass the data from a product custom option( a drop down menu with sizes) in the form, but i can't seem to get it working.

    Its showing in the category list with all the options but when I add to cart it's not passing the value.
    Thank you in advance.

    ReplyDelete
  2. Hi

    This works great except for one problem! When you add the first product to the cart it shows the number of products in the cart on the top right of the page but the amount is 0!!!

    However, when you add the next product, it recalculates and then shows the correct amount.

    The problem is when the first product is added.

    Any ideas?

    ReplyDelete
  3. This is working for simple products. But not working for group products. Any ideas to get this work for group products?

    ReplyDelete
  4. Hi,

    Thanks for sharing this, it's very helpful for me, I need one help in that, can I get 1 qty default on qty box?.

    Thanks in Advance

    ReplyDelete