2

I need to display some order details on a page of my site.

I found the code that does what I need:

function get_order_details($order_id) {
    $order = wc_get_order( $order_id );
    $order_meta = get_post_meta($order_id);

    echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>';
    echo '_billing_first_name: ' . $order_meta[_billing_first_name][0] . '<br>';
    echo '_billing_last_name: ' . $order_meta[_billing_last_name][0] . '<br>';
    echo '_billing_address_index: ' . $order_meta[_billing_address_index][0] . '<br>';
    echo '_shipping_address_index: ' . $order_meta[_shipping_address_index][0] . '<br>';
    echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>';
}

get_order_details(100);

If insert this code into the function.php, it works. Information about the order is inserted on all pages at the top, directly above the header.

I want something else, but I do not know how to do it. The site has a page where there is a form:

<form action="/wp-content/themes/storefront/order_number.php" method="post">
    <label for="order_number">Order number</label><br>
    <input type="text" name="order_number" size="30"><br>
    <input type="submit" id="submit" value="Find and output"><br>
</form>

What I have tried:

1) Add this function in custom file "order_number.php", but it did not work. An error occurs:

Fatal error: Call to undefined function wc_get_order () in Z: \ home \ magazinehard.ru \ www \ wp-content \ themes \ storefront \ order_number.php on line 4.

2) Add "include 'functions.php'" in "order_number.php". An error occurs:

Fatal error: Call to undefined function wp_get_theme() in Z:\home\magazinehard.ru\www\wp-content\themes\storefront\functions.php on line 11.

How can I make this function work in the file "order_number.php". Or it is possible to use only the necessary function from a file "functions.php", and others to ignore?

If all this is not possible, are there any other ways to display the details of the order on the page?

I am not really skilled in PHP, but I needed to write a diploma project.

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Den Smiryagin
  • 23
  • 1
  • 3

1 Answers1

1

Updated - Try the following instead:

// Function that output order details
function display_order_details() {
    // Exit if Order number not submitted
    if ( ! isset( $_POST['order_number'] ) ) 
        return; // Exit

    if( $_POST['order_number'] > 0 )
        $order_id = sanitize_text_field( $_POST['order_number'] );
    else
        return; // Exit

    ## $order = wc_get_order( $order_id ); // Not really needed in this case

    echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>
        <p>';

    $billing_first_name = get_post_meta( $order_id, '_billing_first_name', true );
    if( ! empty( $billing_first_name ) )
        echo 'Billing first name: ' . $billing_first_name . '<br>';

    $billing_last_name = get_post_meta( $order_id, '_billing_last_name', true );
    if( ! empty( $billing_last_name ) )
        echo 'Billing last name: ' . $billing_last_name . '<br>';

    $billing_address_index = get_post_meta( $order_id, '_billing_address_index', true );
    if( ! empty( $billing_address_index ) )
        echo 'Billing details: ' . $billing_address_index . '<br>';

    $shipping_address_index = get_post_meta( $order_id, '_shipping_address_index', true );
    if( ! empty( $shipping_address_index ) )
        echo 'Shipping details: ' . $shipping_address_index;

    echo '</p><br>
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>';
}

// Shotcode that display the form and output order details once submitted
add_shortcode( 'order_details', 'form_get_order_details' );
function form_get_order_details(){
    ob_start(); // Buffering data

    ?>
    <form action="" method="post">
        <label for="order_number">Order number</label><br>
        <input type="text" name="order_number" size="30"><br><br>
        <input type="submit" id="submit" value="Find and output"><br>
    </form>
    <?php

    display_order_details();

    return ob_get_clean(); // Output data from buffer
}

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


POSSIBLE USAGE:

  1. In any page (or post) you can use the shortcode [order_details] in the WordPress text editor.
  2. In a php file you will use: echo do_shortcode("[order_details]");
  3. In a php file in the html, you will use: <?php echo do_shortcode("[order_details]"); ?>

It should work and display order details once the order number will be submitted

enter image description here

Related thread: How to get WooCommerce order details

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you very much for your help. Everything works great. – Den Smiryagin Apr 18 '18 at 13:44
  • Prompt, please: it is possible to deduce result of request not below, and on other page? – Den Smiryagin Apr 19 '18 at 00:05
  • @DenSmiryagin Yes it's… You can have 2 short codes. one for the form and one for the displayed results... If you don't know how to do it you should ask a new question and notify me here. – LoicTheAztec Apr 19 '18 at 09:02
  • Thank you very much. I want to try it myself and if it does not work, then I'm will ask a new question. – Den Smiryagin Apr 19 '18 at 10:31
  • Hello. Tell me more please about your code. He works perfectly. But I wanted to know: I realized that all the data is taken from the "wp_postmeta". But in this table there are no rows with the goods that the client bought. Can I display information about the purchased product in the same way as in your code or do I need to do another function? – Den Smiryagin Apr 21 '18 at 19:33