1

The code below re-displays each image without resetting at each foreach loop.

For example it gives:

  • product 1 - image1
  • product 2 - image1+2
  • product 3 - image1+2+3 ...

How can I make sure that the pictures of the past products are not displayed again for the following products?

Expected result:

  • product 1 - image 1
  • product 2 - image 2
  • product 3 - image 3 ...

Thank you in advance, here is my code:

    <?php foreach ( $last_order->get_items() as $item ) : ?>
        <?php 
   $product   = $item->get_product(); // Get the WC_Product object (from order item)
    $thumbnail = $product->get_image(array( 50, 50)); // Get the product thumbnail (from product object)
    if( $product->get_image_id() > 0 ){
        $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
}
 echo $item_name . $item->get_name();?>
    <?php endforeach;?>
Ruvee
  • 8,611
  • 4
  • 18
  • 44
Loris GFT
  • 87
  • 6

1 Answers1

3

It's because you have an extra variable called $item_name at the end of your $item_name variable! Replace you code with the following code to fix the duplicates:

foreach ($last_order->get_items() as $item) :
  $product   = $item->get_product(); 
  $thumbnail = $product->get_image(array(50, 50));
  if ($product->get_image_id() > 0) {
    $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>'; // You had an extra variable here
  }
  echo $item_name . $item->get_name();
endforeach;

Tested and works! Let me know if you were able to get it to work too!

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • 1
    Thank you very much it works! (There is just a double $$ on line 1, but I still managed to correct this detail myself ) – Loris GFT Sep 10 '21 at 09:19
  • Great! Yes it was a typo because i tested it on a different variable on my end and i forgot to remove it. I just updated my answer. – Ruvee Sep 10 '21 at 09:28