Author Topic: Sum quantity field if value meets certain criteria.  (Read 220 times)

mailblade

  • Jr. Member
  • **
  • Posts: 56
  • A beginner
Sum quantity field if value meets certain criteria.
« on: October 12, 2017, 09:48:48 am »
Good day

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

Code: [Select]
<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:
Code: [Select]
<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:

Code: [Select]
<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

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2410
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Re: Sum quantity field if value meets certain criteria.
« Reply #1 on: October 12, 2017, 13:22:22 pm »
Your script is totally wrong or bad
Code: [Select]
$(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
Quote
jQuery(document).ready(function($){

Quote
if (document.getElementById("uom_value").value == "CRT"){ //Where "uom_value" is "CRT"
better use
Quote
var 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

  • Jr. Member
  • **
  • Posts: 56
  • A beginner
Re: Sum quantity field if value meets certain criteria.
« Reply #2 on: October 12, 2017, 13:28:46 pm »
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.

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2410
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Re: Sum quantity field if value meets certain criteria.
« Reply #3 on: October 12, 2017, 13:31:23 pm »

mailblade

  • Jr. Member
  • **
  • Posts: 56
  • A beginner
Re: Sum quantity field if value meets certain criteria.
« Reply #4 on: October 12, 2017, 14:16:05 pm »
Quote
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.

Can I use a class instead?

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2410
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Re: Sum quantity field if value meets certain criteria.
« Reply #5 on: October 12, 2017, 14:39:05 pm »
YOu can use anything but not ID, it's the only exception in javascript, as H1 for seo.

mailblade

  • Jr. Member
  • **
  • Posts: 56
  • A beginner
Re: Sum quantity field if value meets certain criteria.
« Reply #6 on: October 12, 2017, 14:53:08 pm »
Thanks a lot, appreciate the tips and advice.