Skip to content

Instantly share code, notes, and snippets.

@sh-sabbir
Last active July 3, 2022 03:16
Show Gist options
  • Save sh-sabbir/372cb38ca499a185ae7120b0097774e6 to your computer and use it in GitHub Desktop.
Save sh-sabbir/372cb38ca499a185ae7120b0097774e6 to your computer and use it in GitHub Desktop.
WooCommerce: Get order_id for existing order in checkout page
<?php
/*
* Context:
* Sometime we may need to get the `order_id` in checkout page. When redirecting from `order-pay` you can grab it from `query_var`. But as soon as you reload the page it's lost.
*/
// You can access it from WooCommerce Session. For pending order there is a key named `order_awaiting_payment` in WooCommerce Session.
// Method 1: using `WC_Session_Handler`
$wc_session = new WC_Session_Handler();
$wc_session_data = $wc_session->get_session_data();
// Check if there is order_id or set it as 0
$order_id = isset($wc_session_data['order_awaiting_payment'])?$wc_session_data['order_awaiting_payment']:0;
// Method 2: accessing `WC_Session` directly using `WC()`
$order_id = WC()->session->get( 'order_awaiting_payment' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment