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

Tuesday, April 12, 2022

[FIXED] Woocommerce - How to fix Uncaught Error: Call to a member function get_image() on bool in

 April 12, 2022     html, php, shortcode, woocommerce, wordpress     No comments   

Issue

I have this shortcode which returns information of all orders placed by a user. Works well! However, I have come to the point of introducing some $product variable to call images, download button etc. When I do this I get the following error:

Fatal error: Uncaught Error: Call to a member function get_image() on bool in /home/vwzidcur/public_html/wp-content/themes/astra-child/woocommerce/woo-shortcodes.php:46. On line 46 of my file I have this: $order_img = $product->get_image();

I followed this guide https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/ and other tips here on stack to structure the shortcode. But now I don't understand what I'm doing wrong and why I'm getting that error. Can anyone light my way?

The piece of code I'm working on is this:

// THIS IS WHERE I STRUGGLE
   $product = $item->get_product(); 
   $order_img = $product->get_image(); 

This is Shortcode

// Give the callback function a clear and descriptive name.
add_shortcode( 'prcsed_order_name' , 'prcsed_order_2' );
function prcsed_order_2() {
 
// Get all orders for the current user.
$customer = wc_get_orders(['customer_id' => get_current_user_id(),]);

   foreach ( $customer as $order ) {
   foreach ( $order->get_items() as $item_id => $item ) {
       
   // Get Order info elements
   $order_id = $order->get_id();
   $status = $order->get_status();
   $date_created = $order->get_date_created()->date('d/m/Y - H:i');
   $payment_method = $order->get_payment_method_title();
   $order_total = $order->get_formatted_order_total();
   
   // Get and Loop Over Order Items
   $product_name = $item->get_name();
   
   // THIS IS WHERE I STRUGGLE
   $product = $item->get_product(); 
   $order_img = $product->get_image(); 
   }
       
   echo 
   '
   <div class="container_orders_img"> '. $order_img .' </div>
   <div class="container_orders_card"> 
   
   <div class="items_orders_card name"> '. $product_name .' </div>
   <div class="items_orders_card"> <span class="items_title">Ordine:</span> #'. $order_id .'  </div>
   <div class="items_orders_card"> <span class="items_title">Effettuato il:</span> '. $date_created .' </div>
   <div class="items_orders_card"> <span class="items_title">'. $payment_method .'</span> • '. $order_total .' </div>
   <div class="items_orders_card"> <span class="items_title">Stato:</span> '. $status .' </div>
   
   </div>
   
   ';  
           
   }
}       
}

Solution to the problem:

Thanks to @Obsidianlab and @Nathan Dawson who gave me the right information I was able to solve my problem. Below I place the piece of code that I modified to fix the problem.

// NOW NOT A PROBLEM ANYMORE :-)
$product = $item->get_product();
    if( $product instanceof WC_Product ){
    $order_img = $product->get_image();
    }

Solution

Duplicate of How to interpret "Fatal error: Uncaught Error: Call to a member function get_price() on boolean in".

Seems like your trying to perform a method on a boolean. It's highly likely that the product id doesn't exist and the method returns false.

Make sure to implement a check that $product = $item->get_product(); doesn't return a boolean before executing the get_image() function.



Answered By - Obsidianlab
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • 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