How to compare values in a loop?

I have orders come in. Each order has a type i.e ‘Vanilla’ and a size i.e ‘Mini with a quantity.

If there are 5 Mini Vanillas I want it to display once not 5 times. I also want to tally the quantity.

Read More

Here is one of the many things I attempted

$prevSize = null;
$prevType= null;

foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
    $order_items = $new_order->get_items();

  foreach ($order_items as $order_item ){
  $currentSize = $order_item['pa_size'];
  $currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
  $currentType = $currentType[0]->name;

  if($prevSize != $currentSize){
    echo $currentType . '<br>';
    echo $currentSize . '<br>';
  }
    $count += $order_item['qty'];
    echo $count;
 }
  $prevSize = $currentSize;
  $prevType = $currentType;}

Related posts

Leave a Reply

1 comment

  1. We have analyzed your query, and according to your need, here is my solution:

    $order_type = array();  
    foreach ($orders as $order){
      echo '<tr>';  
      $new_order = new WC_Order();
      $order_items = $new_order->get_items();
      $count = 0;
      foreach ($order_items as $order_item ){
        $currentSize = $order_item['pa_size'];
        $currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
        $currentType = $currentType[0]->name;
        $temp_order_type = $currentSize.'_'.$currentType;
        if(!in_array($temp_order_type, $order_type)){
           $order_type[] = $temp_order_type;
           echo $currentType . '<br>';
           echo $currentSize . '<br>';
        }
        $count += $order_item['qty'];
      }
      echo $count;
    }  
    

    Above code will display each order type once not display multiple times. And display the quantity ordered in each order.