getting Woocommerce order Number in Gravity Form

I am working on a gravity form where I need to get the Order Number from Woocommerce. When a user place an successful order the Order number should be passed to the form so that the user can manage further things for that order.
I am using these plugins:
1. Woocommerce
2. Gravity Form
3. Gravity Form Product Add-on

I need to display the Order number in a Gravity Form Select filed dynamically as in below:

Read More
<select>
  <option value="">Please Select the Order Number</option>
  <option value="28">28</option>
  <option value="30">30</option>
</select>

Here is the code below which I used to get this functionality from a wordpress template file. For this template page I am using Gravity form shortcode to display the form. This code is making a select tag class=”hide” with the order number for that loggedIn user and I am hiding it using css and calling the function clone and append so that the field value is cloned and attached to the gravity form select filed 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 disadvantages and I know it’s not the perfect way. I want functions.php 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;
}

Related posts

1 comment

  1. The functionality to load options dynamically in your case can be achieve using the following approach.

    • A php function which will return the current user’s all order numbers.
    • A ajax call to this function with interval set to some x secs.(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 options for the dropdown.

Comments are closed.