Title: Help with implementing PHP code
Last modified: May 11, 2020

---

# Help with implementing PHP code

 *  [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/)
 * Hey WooCommerce team,
 * I am trying to implement the solution suggested by this user:
    [https://stackoverflow.com/questions/46688978/how-can-i-get-the-latest-order-id-in-woocommerce](https://stackoverflow.com/questions/46688978/how-can-i-get-the-latest-order-id-in-woocommerce)
 * It clearly indicates that the first portion of the code should be in the functions.
   php
 *     ```
       function get_last_order_id(){
           global $wpdb;
           $statuses = array_keys(wc_get_order_statuses());
           $statuses = implode( "','", $statuses );
   
           // Getting last Order ID (max value)
           $results = $wpdb->get_col( "
               SELECT MAX(ID) FROM {$wpdb->prefix}posts
               WHERE post_type LIKE 'shop_order'
               AND post_status IN ('$statuses')
           " );
           return reset($results);
       }
       ```
   
 * However, where should I be replacing the second half of the code?
 *     ```
       $latest_order_id = get_last_order_id(); // Last order ID
       $order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order oject
       $order_details = $order->get_data(); // Get the order data in an array
   
       // Raw output test
       echo '<pre>'; print_r( $order_details ); echo '</pre>';
       ```
   
 * I tried placing it in functions.php but it throws up an error message.
 * Thanks.
    -  This topic was modified 6 years ago by [arthurlam1](https://wordpress.org/support/users/arthurlam1/).
    -  This topic was modified 6 years ago by [arthurlam1](https://wordpress.org/support/users/arthurlam1/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/#post-12815138)
 * [@arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * In the template file where you want this output to display.
 *  Thread Starter [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/#post-12815173)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) Where do I
   find the template file?
 * I want the output to display on the upsell page that comes after the checkout
   page.
 * Thanks.
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/#post-12815205)
 * [@arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * Temporarily install this plugin, and it will display to the Admin what template
   is being used to render :
    [https://en-ca.wordpress.org/plugins/which-template-am-i/](https://en-ca.wordpress.org/plugins/which-template-am-i/)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/#post-12817169)
 * If the template involved happens to be a WooCommerce template, think twice about
   altering templates. While WooCommerce does offer a mechanism of overriding its
   templates, managing updates can be a bit troublesome. In many cases WooCommerce
   templates offer action hooks you can use to alter output without altering templates.
   Updates work much better when hooks are used.
 * Unless you have a custom theme, altering theme templates or even functions.php
   is not recommended. It’s better to create a child theme to contain your custom
   code and templates.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Help with implementing PHP code’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code-2/#post-12817169)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
