add_filter( 'gform_pre_render_2', 'newchip_gform_pre_render' ); function newchip_gform_pre_render( $form ) { $form_id = $form['id']; if ( empty( $_POST[ 'is_submit_' . $form_id ] ) && class_exists( 'WooCommerce' ) ) { $order_statuses = array('wc-on-hold', 'wc-processing', 'wc-completed'); $customer_user_id = get_current_user_id(); // current user ID here for example // Getting current customer orders $customer_orders = wc_get_orders( array( 'meta_key' => '_customer_user', 'meta_value' => $customer_user_id, 'post_status' => $order_statuses, 'numberposts' => 1 ) ); foreach($customer_orders as $order ){ // Order ID (added WooCommerce 3+ compatibility) $order_created_date = $order->get_date_created(); $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id; // Iterating through current orders items foreach($order->get_items() as $item_id => $item){ $product_name = method_exists( $item, 'get_name' ) ? $item->get_name() : $item['name']; } } foreach ( $form['fields'] as &$field ) { if ( $field->id == 6 ) { $field->defaultValue = $product_name; } if ( $field->id == 7 ) { $field->defaultValue = $order_created_date; } } } return $form; }