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 > '. $order_img .' </div>
<div >
<div > '. $product_name .' </div>
<div > <span >Ordine:</span> #'. $order_id .' </div>
<div > <span >Effettuato il:</span> '. $date_created .' </div>
<div > <span >'. $payment_method .'</span> • '. $order_total .' </div>
<div > <span >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();
}
CodePudding user response:
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.
CodePudding user response:
As you've already highlighted, the issue with your code is here:
$product = $item->get_product();
$order_img = $product->get_image();
You're calling the get_product()
method on your order item. The next line assumes that you got a WC_Product
object back however the method will return false if the product isn't found.
You need to make sure that you have an object and not a false value before attempting to call get_image()
.