integris
integris

Reputation: 188

Why does my javascript not update quantity in Opencart category.tpl

In category.tpl Opencart 1.5.x I'm trying to add quantity to the add to cart button.

<div class="cart">
    <input type="text" name="quantity<?php echo $product['product_id']; ?>" value="1" size="1" />
    <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>', 'document.form.quantity<?php echo $product['product_id']; ?>.value')" class="button" />
</div>

If I just put a number in for the second variable of addToCart like

addToCart('<?php echo $product['product_id']; ?>', 2);

This adds quantity 2 fine. Checking the element name in ff shows me the number is coming back from php correctly "quantity6.value", etc. Apparently, but when I click Add to Cart nothing happens. Can someone point me in the right direction?

Just to make it more succinct I changed the line to: " onclick="addToCart('', document.getElementById('quantity').value)" class="button" />

But still the button does not appear to do anything on a click. I assume the document.getElementById().. line is not returning the number properly, but I'm not sure of a good way to test it.

Neither does this seem to work:

<div class="cart">
    <input type="text" name="quantity<?php echo $product['product_id']; ?>" value="1" size="1" onchange="updateQty(<?php echo $product['product_id']; ?>);" />

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

<script type="text/javascript"><!--

function getQty(num) {
getQty = document.getElementById('quantity' & num).value;

}

function updateQty(num) {
var ibox = document.form.quanity[num].value;
quantity[num] = ibox;
}
...
</script>

Upvotes: 0

Views: 2318

Answers (1)

Jay Gilford
Jay Gilford

Reputation: 15151

This seems a little overdone to be honest. You can do this with one function simply by changing the function on the onclick event to addToCartCategory() and changing the name to an ID for the quantity box. Something along the lines of

<div class="cart">
    <input type="text" id="quantity<?php echo $product['product_id']; ?>" value="1" size="1" />

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

<script type="text/javascript"><!--

function addToCartCategory(product_id) {
    var qty = $('#quantity' + product_id).val();
    addToCart(product_id, qty);

}
...
</script>

Upvotes: 2

Related Questions