Reputation: 21
I made this shortcode to show the total value of an order to users.
It does work to display the value, however it keeps giving me this error message when I try to access my website "There has been a critical error on this website. Please check your site admin email inbox for instructions."
The error seems to be in this line $order_line_items = $order->get_items();
.
Error details emailed to me by Wordpress.
An error of type E_ERROR was caused in line 67 of the file /home/example.com/wp-content/themes/hello-theme-child-master/functions.php. Error message: Uncaught Error: Call to a member function get_items() on bool in /home/ example.com/wp-content/themes/hello-theme-child-master/functions.php:67
Stack trace:
#0 /home/ example.com/wp-includes/shortcodes.php(356): getOrderItemList('', '', 'order-line-item')
#1 [internal function]: do_shortcode_tag(Array)
#2 /home/example.com/wp-includes/shortcodes.php(228): preg_replace_callback('/\\[(\\[?)(order\\...', 'do_shortcode_ta...', '
Code
function getOrderItemList() {
// get order ID.
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
$total = $order->get_total();
return $total;
}
add_shortcode( 'order-line-item', 'getOrderItemList' );
Thank you
Upvotes: 0
Views: 256
Reputation: 108851
It seems likely that wc_get_order() returned false
. That's WordPress (and php) convention for "I can't find what you want". In this case it likely means there's no order with the number $order_id
.
Edit Try using $order_id = get_query_var( 'order-received' );
to get the order id.
Maybe something like this will help.
function getOrderItemList() {
$order_id = get_query_var( 'order-received' );
if ( ! $order_id ) {
return '';
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
return '';
}
$order_line_items = $order->get_items();
$total = $order->get_total();
return $total;
}
For the sake of troubleshooting you could put return 'bogus $order'
in place of return ''
.
Upvotes: 1