Telmo
Telmo

Reputation: 71

How to retrieve order ID on Order Pay page in WooCommerce

I am trying to modify the Woocomerce checkout page for orders that are failed or pending payment. On those orders the system comes to the checkout with an order already generated, but I am not able to retrieve the ID of that order from the form_checkout.php template.

Failed orders can be paid by the customer with the Woocommerce URL, similar to the following one:

https://myurl.com/checkout/order-pay/XXXXX/?pay_for_order=true&key=wc_order_XXXXXXXXXXXX&subscription_renewal=true

Neither do I retrieve the variables with a $_GET since the URL is transformed into https://myurl.com/checkout when accessing. I don't know if you lose this data or where I can collect it.

Is this possible?

Upvotes: 5

Views: 2878

Answers (2)

Akbarali
Akbarali

Reputation: 904

$order = wc_get_order(get_query_var('order-pay'));

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253784

To retrieve the order Id on Order pay page you will use the following:

global $wp;

if ( isset($wp->query_vars['order-pay']) && absint($wp->query_vars['order-pay']) > 0 ) {
    $order_id = absint($wp->query_vars['order-pay']); // The order ID

    $order    = wc_get_order( $order_id ); // Get the WC_Order Object instance
}

Note: To target Order pay page, you can use is_wc_endpoint_url('order-pay') conditional tag.

Upvotes: 5

Related Questions