skip to Main Content

Below I have 2 foreach loops I’ll like to display the first foreach loop values in my second foreach loop but whenever i try only the first array value gets displayed. below is my code

<?php
  $order_id = $order->get_id();
                                                 
  $serial_numbers = WC_Serial_Numbers_Query::init()->from( 'serial_numbers' )->where( 'order_id', intval( $order_id ) )->get();
  foreach ($serial_numbers as $serial_number) {
      $decrypted_key = WC_Serial_Numbers_Encryption::maybeDecrypt( $serial_number->serial_key ); 
  }

echo '
  foreach ($order->get_items() as $key => $lineItem) {
      $product_id = $lineItem['product_id'];
      $product = wc_get_product( $product_id );
    <div>
      <table>
        <tbody>
                  <tr>
                  <td>
                  <div>' . $lineItem['name'] . '</div>
                      </td>
                       </tr>
                        <tr>
                         <td>
                         <div>S/N:' . $decrypted_key . '</div>
                         </td>
                         </tr> 
</tbody>
</table>  ';} ?>

2

Answers


  1. To display variables values you need to use something called interpolation.

    to interpolate (insert) a variable and showcase it inside of a string you need to use double-quotes (").

    For example:

    $a = "Hello world";
    echo '$a!';
    

    will echo ‘$a!’ literally, opposite to:

    $a = "Hello world";
    echo "$a!"
    

    will echo "Hello world!"

    So give it a shot with double quotes instead of single-quotes.

    Login or Signup to reply.
  2. If you want to display all of the decrypted serial keys for each WC_Order_Item. You should place the first foreach loop within the second one.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search