2

I have a problem on the "my account" page of my woocommerce store. Indeed, when I log in with a new customer account that has never placed an order, my dashboard has display bugs with disappearing containers.

After testing, the bug disappears only if I place an order with the account in question.

After performing a series of tests, the piece of code that produces the bug is the following (it displays the last command of the connected user):

<?php 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;
?>

Do you have any idea where this could be coming from?

In advance, Thank you very much for your help.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Loris GFT
  • 87
  • 6
  • you must check if both `$last_order` and `$last_order->get_items()` are assigned, and check also `get_items()` array length before foreach loop – tdjprog Nov 01 '21 at 23:26

1 Answers1

2

Not sure where this comes from, but it's trying to loop through the $last_order variable.

So in order to prevent the loop from running for users who have never placed an order (which means $last_order is false), you could wrap it inside an if statement and check whether $last_order is false or not, like this:

if($last_order){
  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;
}

Let me know if it works for you!

Ruvee
  • 8,611
  • 4
  • 18
  • 44