JSFiddle - React, Tailwind, and code Playground
by lindychen
JavaScript
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);
}
function your_shortcode_function() {
$latest_order_id = get_last_order_id(); // Last order ID
echo '<pre>'; print_r( $latest_order_id ); echo '</pre>';
// EDIT: if you like to use the order id as javascript variable, you can do this in the following way
?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
var order_id = <?php echo $latest_order_id; ?>;
console.log(order_id);
});
</script>
<?php
}
add_shortcode( 'your_shortcode', 'your_shortcode_function' );