2

Making a custom thank you page template, I want to display order number if I echo $order = new WC_Order($post_id); it returns values 0 or empty strings: but the permalink has the order id wonder what could be causing this ?

<strong><?php echo $order = new WC_Order($post_id); echo $order->get_order_number(); ?></strong>

The whole code is below:

<p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>

<ul class="order_details">
<li class="order">
    <?php _e( 'Order number:', 'woocommerce' ); ?>
    <strong><?php echo $order = new WC_Order($post_id); echo $order->get_order_number(); ?></strong>
</li>

</ul>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Peter33
  • 59
  • 1
  • 10

1 Answers1

1

Your problem related to WooCommerce checkout/thankyou.php template is:

  • First you can't use echo with $order = new WC_Order($post_id); as it's an object, but not a string.
  • Second, the $order object already exists in thank you template, so you don't need to get it again. You can see that on your first code line:
<p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>

So your code will be simply:

<p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>

<ul class="order_details">

    <li class="order">
        <?php _e( 'Order number:', 'woocommerce' ); ?>
        <strong><?php echo $order->get_order_number(); ?></strong>
    </li>

</ul>

This should work…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I have tried this before as it was my first solution, but it gives me 'Fatal error: Uncaught Error: Call to a member function get_order_number() on string' any ideas ? – Peter33 Jul 18 '17 at 08:43
  • its a new page, wc version 3.1.0. what I did made a new page on settings created php template files and pasted that code in. and did a redirect in functions.php will pasted that below. – Peter33 Jul 18 '17 at 08:53
  • function wc_custom_redirect_after_purchase() { global $wp; if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) { $order_id = absint( $wp->query_vars['order-received'] ); $order_key = wc_clean( $_GET['key'] ); $redirect = get_permalink (get_page_by_title( "Thank-You" )->ID ); $redirect .= get_option( 'permalink_structure' ) === '' ? '&' : '?'; $redirect .= 'order=' . $order_id . '&key=' . $order_key; wp_redirect( $redirect ); exit; } } – Peter33 Jul 18 '17 at 08:56
  • let me know if that helps – Peter33 Jul 18 '17 at 08:56
  • You should better override **existing thankyou template** as you will face some big problems as you can see. If you don't know about **overriding WooCommerce tempates via the theme**, read [**this official short documentation**](https://docs.woocommerce.com/document/template-structure/) … We don't know why you are making a custom new template, please explain it, updating your question (what are you trying to do and why using this way?)… – LoicTheAztec Jul 18 '17 at 08:58
  • 1
    understood, yeah I know how to edit them will do that instead of making new one as it keeps causing problems – Peter33 Jul 18 '17 at 09:07