Question

Gravity Forms Product Add-ons for Woocommerce displays the form inputs of a user in the cart and checkout. I want to display these form fields in the user's Account page with their orders. I've been trying for a week and can't seem to get the right php code put together. How can I do this?

My code as it stands is like so:

$form_id = RGFormsModel::get_form_id('Domains');
$form = GFFormsModel::get_form_meta($form_id);
$field = GFFormsModel::get_field($form, 1);
$leads = RGFormsModel::get_leads($form['id']);

foreach($leads as $lead)
{
    foreach($field as $field_id)
    {
        $value = rgar($lead, (string) $field_id);
        echo $value;
    }
}

This returns all entries for the field I want, however, I only want to return the entry that was submitted with that particular product. Help?

Here's a quick example for clarity on what I'm looking for. A user buys a shirt and selects size large from a Gravity Form that was attached to the product. On the cart and checkout pages, it says beneath the product title "Size: Large". I want to add that same text to the "My Account" page with their order.

Was it helpful?

Solution

The way I solved a similar problem may not be the best in coding practices but it works. It takes custom data associated with the order from Gravity Forms and displays it under the shipping address in WooCommerce individual order page:

<?php
add_action('woocommerce_admin_order_data_after_shipping_address', 'ips_show_signs_in_admin_under_shipping', 10, 1);
/*
function to put in functions.php of the theme to add custom gravityforms data to customer order page in admin area.
*/
function ips_show_signs_in_admin_under_shipping($order)
{
    global $woocommerce, $post;
    $order_items = $order->get_items(apply_filters('woocommerce_admin_order_item_types', array( 'line_item')));
    foreach($order_items as $item_id => $item)
    {
        if ($metadata = $order->has_meta($item_id)) //not a comparison, so one equal sign. so if there is any data in there it would assign it and thus making it true, if no data it would be false
        {
            foreach($metadata as $meta)
            {
                // Skip hidden woocommerce core fields just in case
                if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array(
                    '_qty',
                    '_tax_class',
                    '_product_id',
                    '_variation_id',
                    '_line_subtotal',
                    '_line_subtotal_tax',
                    '_line_total',
                    '_line_tax',
                    )))) continue;
                if (is_serialized($meta['meta_value'])) continue; // Skip serialised meta since we dont need it incase its in there
                $meta['meta_key'] = esc_attr($meta['meta_key']); //just to make sure there is no surprises in there
                $meta['meta_value'] = esc_textarea($meta['meta_value']); // using a textarea check for surprises (sql injection)
                if ("Signpost Address" === $meta['meta_key']) //here comes the custom data from gravity forms
                {
                   echo $meta['meta_value']; //this is what i was after from gravity forms collected with order
                }
            } //closes --- foreach($metadata as $meta)
        } //closes --- if ($metadata = $order->has_meta($item_id))
    } //closes --- foreach($order_items as $item_id => $item)
} //closes --- function
?>

Edit: This code searches the order meta data

OTHER TIPS

I have added the above code in my functions.php file and unfortunately this hasn't worked for me. I have looked all over for a solution to this and this seems to exactly be what I'm after. Do i simply replace "Signpost Address" with say "Blanket Size" if that were one of my gravity form options?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top