Mister Karl asked me today if I knew of any methods with OpenCart to add the Quantity field (qty) to the category page, that is the products listed either in a grid or list format.  While I have previously achived this with nasty horrible server-side hacks, I thought it was probably more appropriate to find a more consistent solution that would stay present even after updating OpenCart.

Turns out, the nicest place for me to achieve this fix, was in the Theme file itself.  This fix works in version of OpenCart and changes only one file.

The file you'll need to edit (assuming you're working with the default theme in OpenCart) is /catalog/view/theme/default/template/product/category.tpl

After you've seen the simple changes made, you should be able to adapt this to any theme you like.

<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" />

Change this line to

<input type="text" value="1" class="item-<?php echo $product['product_id']; ?>" />
<input type="button" value="<?php echo $button_cart; ?>" onclick="addQtyToCart('<?php echo $product['product_id']; ?>');" class="button" />

We've added in a simple text input, given it a class of item-ProductId, and changed the Add to Cart button to call addQtyToCart instead of addToCart

Add a new line after the opening of "script"

function addQtyToCart(product_id) {
  var qty = $('.item-' + product_id).val();
  if ((parseFloat(qty) != parseInt(qty)) || isNaN(qty)) {
    qty = 1;
  addToCart(product_id, qty);

Basically, you could do this without the "add qty to cart" function I've made, but I wanted to add in a little bit of error checking before adding something to the cart. If it's not a number, the qty will reset to 1 and continue to add the item anyway. It still uses the function addToCart, but with the additional parameter which defines the quantity to add to the cart.

Hope this helps someone out with quickly adding this to their OpenCart system, I did some googling prior to starting but sadly every solution I found demanded you pay at least $30. Which for an open-source product I thought was a little harsh of the community. Actually I thought it was pretty rude really, and down-right wrong. Don't get me wrong, I work on paid software solutions, but if you're going to use someone else's entire solution and add on a little "spit" of a feature.... Anyway. ENJOY.