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
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:
will echo ‘$a!’ literally, opposite to:
will echo "Hello world!"
So give it a shot with double quotes instead of single-quotes.
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.