Home > Blockchain >  Woocommerce my account page runs an extra loop for new customers
Woocommerce my account page runs an extra loop for new customers

Time:11-03

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 >' . $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.

CodePudding user response:

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 >' . $thumbnail . '</div>'; // You had an extra variable here
    }
    echo $item_name . $item->get_name();
  endforeach;
}

Let me know if it works for you!

  • Related