• Resolved arthurlam1

    (@arthurlam1)


    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

    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 RK a11n

    (@riaanknoetze)

    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

    (@arthurlam1)

    @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 4 years, 6 months ago by arthurlam1.
    Mina

    (@purpleberryservices)

    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

    (@arthurlam1)

    @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

    (@purpleberryservices)

    Yes it will go in functions.php file.

    There is no comma after $url statement.

    IT should be $url = ‘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;;
    wp_redirect( $url.’?id=’.$order_id );
    exit;
    }

    }

    Thread Starter arthurlam1

    (@arthurlam1)

    @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

    (@arthurlam1)

    @purpleberryservices my bad. Managed to solve it!

    Thank you! And yes it works ??

    Mina

    (@purpleberryservices)

    Great!! ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Help with implementing PHP code’ is closed to new replies.