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

---

# Help with implementing PHP code

 *  Resolved [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/)
 * 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.

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

 *  Plugin Support [Riaan K.](https://wordpress.org/support/users/riaanknoetze/)
 * (@riaanknoetze)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12815798)
 * Hi there,
 * When adding all of it to the functions.php file, what error message are you getting?
   Also, would you mind sharing a bit more on what it is you’re trying to do – what’s
   the use-case for that code?
 * Finally, please note that the Stack Exchange example you reference is from 2017
   and might no longer be compatible with the latest version of WooCommerce.
 *  Thread Starter [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12816121)
 * [@riaanknoetze](https://wordpress.org/support/users/riaanknoetze/) Hey RK, sure
   let me explain to you the use case.
 * **Background**
 * We have changed the checkout process slightly in WooCommerce
 * Usually, a user goes: Order Checkout –> Thank you Page (where Order ID appears)–
   > Upsell page
 * Now it goes like this: Order Checkout –> Upsell Page
 * This redirect is achieved using this script:
 *     ```
       add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
       function woo_custom_redirect_after_purchase() {
       global $wp;
       if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
       wp_redirect( 'https://website.com/upsell/' );
       exit;
       }
       }
       ```
   
 * **Question**
 * The issue here is that I can’t extract Order ID from the DOM in the Thank You
   page because after checkout we immediately redirect users to the Upsell page.
 * So is there a way to push the Order ID into a Javascript variable on the Upsell
   Page? Thus, I tried to follow what the Stack Overflow Article did.
 * Thank you.
    -  This reply was modified 6 years ago by [arthurlam1](https://wordpress.org/support/users/arthurlam1/).
 *  [Mina](https://wordpress.org/support/users/purpleberryservices/)
 * (@purpleberryservices)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12817069)
 * Hi,
 * You should try something like this:
 * add_action( ‘woocommerce_thankyou’, ‘pbs_redirect_to_custom_thank_you’);
 * function pbs_redirect_to_custom_thank_you( $order_id ){
    $order = wc_get_order(
   $order_id );
 * if ( ! $order->has_status( ‘failed’ ) ) {
 * $url = // Upsell page URL.
    wp_redirect( $url.”?id=”.$order_id ); exit; }
 * }
 *  Thread Starter [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818435)
 * [@purpleberryservices](https://wordpress.org/support/users/purpleberryservices/)
   Thanks.
 * Believe I should put your code within functions.php?
 * I get this error message: `syntax error, unexpected 'wp_redirect' (T_STRING)`.
   Below is the updated code for the upsell page. Can you let me know what went 
   wrong? Thanks.
 *     ```
       add_action( 'woocommerce_thankyou', 'pbs_redirect_to_custom_thank_you');
   
       function pbs_redirect_to_custom_thank_you( $order_id ){
       $order = wc_get_order( $order_id );
   
       if ( ! $order->has_status( 'failed' ) ) {
   
       $url = 'https://website.com/upsell/'
       wp_redirect( $url.'?id='.$order_id );
       exit;
       }
   
       }
       ```
   
 *  [Mina](https://wordpress.org/support/users/purpleberryservices/)
 * (@purpleberryservices)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818442)
 * Yes it will go in functions.php file.
 * There is no comma after $url statement.
 * IT should be $url = ‘[https://website.com/upsell/&#8217](https://website.com/upsell/&#8217);;
 * ————-
    add_action( ‘woocommerce_thankyou’, ‘pbs_redirect_to_custom_thank_you’);
 * function pbs_redirect_to_custom_thank_you( $order_id ){
    $order = wc_get_order(
   $order_id );
 * if ( ! $order->has_status( ‘failed’ ) ) {
 * $url = ‘[https://website.com/upsell/&#8217](https://website.com/upsell/&#8217);;
   
   wp_redirect( $url.’?id=’.$order_id ); exit; }
 * }
 *  Thread Starter [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818467)
 * [@purpleberryservices](https://wordpress.org/support/users/purpleberryservices/)
   Thanks. I removed the comma at the end and replaced it with a ;
 * The below code is what I am using. I now get an error message `syntax error, 
   unexpected '='`
 *     ```
       add_action( 'woocommerce_thankyou', 'pbs_redirect_to_custom_thank_you');
   
       function pbs_redirect_to_custom_thank_you( $order_id ){
       $order = wc_get_order( $order_id );
   
       if ( ! $order->has_status( 'failed' ) ) {
   
       $url = 'https://website.com/upsell/;
   
       wp_redirect( $url.'?id='.$order_id );
       exit;
       }
   
       }
       ```
   
 *  Thread Starter [arthurlam1](https://wordpress.org/support/users/arthurlam1/)
 * (@arthurlam1)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818473)
 * [@purpleberryservices](https://wordpress.org/support/users/purpleberryservices/)
   my bad. Managed to solve it!
 * Thank you! And yes it works 🙂
 *  [Mina](https://wordpress.org/support/users/purpleberryservices/)
 * (@purpleberryservices)
 * [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818496)
 * Great!! 🙂

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

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

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 8 replies
 * 3 participants
 * Last reply from: [Mina](https://wordpress.org/support/users/purpleberryservices/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/help-with-implementing-php-code/#post-12818496)
 * Status: resolved