0

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

  • As per the [How To Ask](https://stackoverflow.com/help/how-to-ask) guide, which you are encouraged to read before using the site, please don't post images of your code, data or error messages. This info is text. Pasting it as graphics is very impractical as it can't be copied, searched, re-used in answers etc. It makes it difficult for those who might want to help you. Please [edit] your question to include the info as text and use the [formatting tools](https://stackoverflow.com/help/formatting) to present it nicely, so that it is usable for those who want to help you. Thanks – ADyson Aug 05 '22 at 15:42
  • And is there something you didn't understand about the error message? Have you tried to google that type of error message to work out what it means, if you didn't understand fully? basically, if you work it backwards, it's saying you can't call `get_items()` on a boolen. A boolean is either a true or false value. Clearly that's not an object which could have a function. That means $order must be a boolean. So why is it a boolean? Well wc_get_order must have returned a boolean then, since that's where it came from. Why did it do that... – ADyson Aug 05 '22 at 15:45
  • ... well https://wp-kama.com/plugin/woocommerce/function/wc_get_order shows you it can return false, and even shows the source code to understand when that might happen. – ADyson Aug 05 '22 at 15:45
  • Thank you, let me research this. – beatlesfan1234 Aug 05 '22 at 16:14

1 Answers1

1

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

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Thanks O.Jones. Your code results in there being no error messages. However, the shortcode will no longer display my order value. It just turns out blank. – beatlesfan1234 Aug 05 '22 at 16:14
  • Please see my edit. And see this. https://stackoverflow.com/questions/67181999/how-can-i-get-the-order-id-from-the-order-key-in-woocommerce/67182262#67182262 – O. Jones Aug 05 '22 at 16:58