Ben
Ben

Reputation: 3377

Get the order ID on the final checkout page when a customer purchased a product in WooCommerce

I'm developing a plugin that needs to get the order ID on the final checkout page when a customer purchased a product. How do I do that? I tried

global $order
var_dump($order);

And all I see is "DESC". I need to access the current order of the customer who is looking at the page.

I need to get the order id in the frontend and add that ID to a script, also on the front end, so that when a client orders a product and pays, the page will load, show them the "Thank you, here is your order number: xxx". At that moment I need my script to, for example, execute a console.log("The oder ID is:", order_id);

Upvotes: 1

Views: 712

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29650

To get the order ID on the final checkout page when a customer purchased a product you can use the woocommerce_thankyou hook, where the callback function already contains the order ID.

// define the woocommerce_thankyou callback 
function action_woocommerce_thankyou( $order_id ) {
    echo "Thank you, here is your order ID: " . $order_id;

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Customer billing country
    $billing_country = $order->get_billing_country();

    // etc..
}; 
         
// add the action 
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 

Upvotes: 2

Related Questions