How to validate product quantity in cart? 

Post By:user_4885 | Reply

 

Hi,

I am trying to validate the product quantity at checkout page in magento.I want that the product quantity in cart should not be more than 900 for any product.If user click update shopping cart button after increasing the quantity more than 900.It should not allow him to add more than 900 products.

I have used following code for validation in cart.phtml for validation.But when I add more than one product in than cart it does the validation only for the first product not for the other products added in the cart.

code in cart.phtml--->

<button type="submit" name="update_cart_action" value="update_qty" title="<?php echo $this->__('Update Shopping Cart'); ?>" class="button btn-update" onclick="return myFunc()"><span><span><?php echo $this->__('Update Shopping Cart'); ?></span></span></button>                      
                            <
button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Clear Shopping Cart'); ?>" class="button btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Clear Shopping Cart'); ?></span></span></button>                            


<
script type="text/javascript">
//< ![CDATA[
        
function myFunc()
        
{
            
//Validating the  product quantity
            //alert("hello");
            
var myqty=document.getElementById("qtyValid").value;
            if(
myqty>900)
            
{
                alert(
"Maximum allowed quantity is 900.");
                
document.getElementById("qtyValid").value="1";//setting to default value 1
                
return false;
            
}
        }    
//]]>
</script>

qtyValid is text box name.

Please help
Thanks

Image Attachments
New Bitmap Image.jpg
user_7661Tue, 16 Oct 2012

 

Hi,

We mean something like this:

<script>
var 
qty_fields = $$(".qty");
var 
max_qty 1;
var 
error '';
for (var 
0qty_fields.lengthi++) {
    
if (qty_fields[i].value max_qty{
        error 
"Maximum allowed quantity is "+max_qty+".";
        
qty_fields[i].value max_qty;
    
}
}

if (error.length{
    alert(error)
;
}
</script>

user_4885Tue, 16 Oct 2012

 

@BelVG

Can you please share some example for the solution you mentioned?

Thanks.

user_7661Fri, 12 Oct 2012

 

Hi,

You use the document.getElementById("qtyValid") which returns only 1 element.
Use

$$("input[name="ement_name"]")

`Reply Or Comment!
Please LogIn to Reply!