PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] Woocommerce my account page runs an extra loop for new customers

 January 18, 2022     orders, php, woocommerce, woocommerce-theming, wordpress     No comments   

Issue

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.


Solution

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!



Answered By - Ruvee
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing