News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Sum quantity field if value meets certain criteria.

Started by mailblade, October 12, 2017, 09:48:48 AM

Previous topic - Next topic

mailblade

Good day

I have this piece of code in my "default_pricelist.php" which displays a customfield value in the cart:


<td id="uom_value">
<?php  foreach($prow->customfields as $customfields){
   
   
   if(
$customfields->virtuemart_custom_id==20){
   
   echo ( 
$customfields->customfield_value);
  
// var_dump($customfields);
   
}
   }; 
   
?>

</td>


Now, let's give customfield_id(20) a value of either "PLT" or "CRT". In this hypothetical scenario, I added 5 items in the cart.

Items 1 & 2 have "PLT" as a customfield_value - Their respective quantities are 5 & 3.

Items 3, 4 & 5 have "CRT" as a customfield_value - Their respective quantities are 3, 2 & 4.

Now, I have these two textboxes:

<input type="text" id="plt_sum" readonly/>
<input type="text" id="crt_sum" readonly/>


How can I sum the quantity for each product in the cart where the customfield_value is either "PLT" or "CRT"?

So when the user opens the cart, the last two rows will be:

"PLT Quantity: #plt_sum "
"CRT Quantity: #crt_sum"

Here is the basic javascript layout of what needs to be calculated:


<script type="text/javascript">
$(document).ready(function(){
if (document.getElementById("uom_value").value == "CRT"){ //Where "uom_value" is "CRT"
document.getElementById("plt_sum").value == (sum of $qtt); // plt_sum value must add all the quantity values for every item in the cart where "UOM" is "PLT"
}
    else if (document.getElementById("uom_value").value == "PLT"){ //Where "uom_value" is "CRT"
document.getElementById("crt_sum").value == (sum of $qtt); // crt_sum value must add all the quantity values for every item in the cart where "UOM" is "CRT"
}

});
</script>


So I know the code is mostly wrong, since I'm not sure how to loop these values with Javascript.

Thank you for reading and I'd really appreciate any help!

:)

Studio 42

Your script is totally wrong or bad
$(document).ready(function(){
if (document.getElementById("uom_value").value == "CRT"){ //Where "uom_value" is "CRT"
document.getElementById("plt_sum").value == (sum of $qtt); // plt_sum value must add all the quantity values for every item in the cart where "UOM" is "PLT"
}
    else if (document.getElementById("uom_value").value == "PLT"){ //Where "uom_value" is "CRT"
document.getElementById("crt_sum").value == (sum of $qtt); // crt_sum value must add all the quantity values for every item in the cart where "UOM" is "CRT"
}

});

Quote$(document).ready(function(){
use
QuotejQuery(document).ready(function($){

Quoteif (document.getElementById("uom_value").value == "CRT"){ //Where "uom_value" is "CRT"
better use
Quotevar v = $('#uom_value').val();
if(v== "CRT"){
...
}else if (v== "PLT"){
...
}
But you speak (sum of $qtt), if you need to "sum" you need to get all values with a foreach and in this case you cannot use an ID, becaus only fist element having same ID can be used in javascripts.

mailblade

I realise it's bad. I just put it there as pseudo code since I just wanted to outline the logical flow of the function.

Thanks for your suggestions. I'll try them out. I think I'll also have to use the list function for $prow variable, and then use a foreach to calculate the value.


mailblade

QuoteBut you speak (sum of $qtt), if you need to "sum" you need to get all values with a foreach and in this case you cannot use an ID, becaus only fist element having same ID can be used in javascripts.

Can I use a class instead?

Studio 42

YOu can use anything but not ID, it's the only exception in javascript, as H1 for seo.

mailblade