1

I am trying to get order details to display a summary on my thank you page. The issue I am having is that this code snippet I have is breaking my wordpress. I have the stacktrace but I am unsure on how to fix it. The code to me looks correct so ensure why it's not working. Does anybody have any idea what is the meaning of this stacktrace and how I can fix it?

   An error of type E_ERROR was caused in line 7 of the file /home4/xxx/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code. Error message: Uncaught Error: Call to a member function get_items() on boolean in /home4/xxx/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:7
    Stack trace:
    #0 /home4/xxx/public_html/staging/4326/wp-includes/shortcodes.php(343): order_table_summary('', '', 'order_table_sum...')
    #1 [internal function]: do_shortcode_tag(Array)
    #2 /home4/xxx/public_html/staging/4326/wp-includes/shortcodes.php(218): preg_replace_callback('/\\[(\\[?)(order_...', 'do_shortcode_ta...', '\n

My code:

function order_table_summary(){
    
    $order = wc_get_order($order_id);
    
    // Get and Loop Over Order Items
    foreach ( $order->get_items() as $item_id => $item ) {

        echo $item->get_name() . $item->get_quantity. $item->get_total();
    }
}
add_shortcode('order_table_summary', 'order_table_summary');

UPDATE Adding the shortcode

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
  • Where is the `$order_id` defined? hence the error message, the `$order` object does not exist – 7uc1f3r Oct 19 '20 at 07:45
  • 1
    The variable `$order_id` is undefined inside your function. You can read up on [variable scope](https://www.php.net/manual/en/language.variables.scope.php) in the PHP manual. – M. Eriksson Oct 19 '20 at 07:45
  • @7uc1f3r Ah ok, I thought it was a built in variable but I was wrong. Sadly though even when I set this variable under $order as so: $order_id = $order->get_id();, I still get an error in my wordpress – BruceyBandit Oct 19 '20 at 07:52
  • Use it like `$order = wc_get_order( ADD AN EXISTING ORDER ID HERE );` like `wc_get_order( 1 );`Then use `if ( is_a( $order, 'WC_Order' ) ) { $order_status = $order->get_status();` etc.. – 7uc1f3r Oct 19 '20 at 07:55
  • I think the issue I have here (and apologies if I am mistaken). I don't know what the order ids will be. Basically this is an order summary so I want to say get the orderid and from there get the items related to the order. So I don't think I can do: `order = wc_get_order( ADD AN EXISTING ORDER ID HERE );` – BruceyBandit Oct 19 '20 at 07:58
  • Well then you can use the order ID, (post id) if available. For example on the thank you page you have access to the order ID, but before an order has been created (checkout, cart page...) you only have access to existing order IDs, not the current one. In short, it depends on where and how you want to use your shortcode – 7uc1f3r Oct 19 '20 at 08:00
  • Example: [Get the Order ID in Woocommerce order received page as shortcode](https://stackoverflow.com/questions/49605343/get-the-order-id-in-woocommerce-order-received-page-as-shortcode) – 7uc1f3r Oct 19 '20 at 08:02
  • 1
    Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nico Haase Oct 19 '20 at 08:33

1 Answers1

1

The variable $order_id is not defined, $item->get_quantity need to be $item->get_quantity() and the output should never be echoed with a shortcode, always returned.

This shortcode is to be used on Order received page

add_shortcode('order_table_summary', 'display_order_table_summary_thankyou');

function display_order_table_summary_thankyou(){
    global $wp;

    // If order_id is defined on Order reveived / thankyou page
    if ( is_wc_endpoint_url('order-received')
    && isset($wp->query_vars['order-received'])
    && absint($wp->query_vars['order-received']) > 0 ) {

        // Get the WC_Order Object
        $order = wc_get_order( absint($wp->query_vars['order-received']) );

        ob_start(); // Start buffering

        echo '<table><tr>
            <th>' . __("Product name", "woocommerce") . '</th>
            <th>' . __("Quantity", "woocommerce") . '</th>
            <th>' . __("Line total", "woocommerce") . '</th>
        </tr>';

        // Loop Over Order Items
        foreach ( $order->get_items() as $item ) {
            echo '<tr>
            <td>' . $item->get_name() . '</td>
            <td>' . $item->get_quantity() . '</td>
            <td>' . $item->get_total() . '</td>
            </tr>';
        }

        echo '</table>';

        return ob_get_clean(); // Return the buffered content
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

USAGE:

  • [order_table_summary]
  • or also inside PHP code: echo do_shortcode('[order_table_summary]');

Just for testing:

add_action( 'woocommerce_thankyou', 'testing_shorcode' );
function testing_shorcode() {
    echo do_shortcode('[order_table_summary]');
}

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • @BruceyBandit I have added a testing function for the shortcode in my answer. The display will be at the end of thank you page. – LoicTheAztec Oct 19 '20 at 10:31
  • It didn't display the shortcode. It is a page created which I have titled 'Thank You' so don't know if that makes a difference. – BruceyBandit Oct 19 '20 at 10:38
  • Btw the other question. It did work when I tried it but when I refreshed the page, it then says wordpress have encountered an error again. I am trying to see who to debug in wordpress – BruceyBandit Oct 19 '20 at 10:39
  • To debug: https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3/61754061#61754061 (in woocommerce and wordpress) … My answer code here doesn't make any error, so it's something else. – LoicTheAztec Oct 19 '20 at 10:42
  • Thank you, I'm just going through a youttube video that is showing same thing. Hopefully with debug can see what's happening – BruceyBandit Oct 19 '20 at 10:46