Get Woocommerce order number in Gravity format
I am working on a gravity form where I need to get the order number from Woocommerce. When a user makes a successful order, the order number must be passed to the form so that the user can manage further things for that order. I use these plugins: 1. Woocommerce 2. Gravity shape 3. Gravity shape add-on.
I need to display the order number in Gravity Form Select, registered dynamically as shown below:
<select>
<option value="">Please Select the Order Number</option>
<option value="28">28</option>
<option value="30">30</option>
</select>
Below is the code I used to get this function from a wordpress template file. For this template page, I am using the Gravity Form Shortcode to display the form. This code makes a select class = "hide" tag with the order number for that user loggedIn and I hide it with css and call the clone function and add so that the field value is cloned and attached to the gravity form. ID # input_3_2.
<?php if ( is_user_logged_in() ) {
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'publish'
) ) );?>
<?PHP if ( $customer_orders ) : ?>
<select class="hide">
<?php
foreach ( $customer_orders as $customer_order ) {
$order = new WC_Order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
?>
<option value="<?php echo $order->get_order_number(); ?>"><?php echo $order->get_order_number(); ?></option>
<?php
}
?>
<?php endif; ?>
</select>
<script>
jQuery(document).ready(function() {
jQuery('select.hide').find('option').clone().appendTo('#input_3_2');
});
</script>
This code works but has drawbacks and I know this is not the ideal way. I want the .php functions to handle this functionality like the following code, but dynamically.
add_filter('gform_field_content', 'modify_field', 10, 5);
function modify_field($modify_html, $field){
if($field['formId'] == 6){
if($field['id'] == 5){$modify_html = str_replace("<option value='' >Please Select Your Order Number</option>","<option value='27'>27</option><option value='30'>30</option>",$modify_html);}
}
return $modify_html;
}
source to share
The functionality for dynamically loading parameters in your case can be achieved using the following approach.
- Php function that will return all order numbers to the current user.
- Ajax call this function with an interval set to some x sec. (This will call the php function to run every second to check with the server for all order numbers).
- In the set interval function, update all the dropdown options.
source to share