user621639
user621639

Reputation:

Get gravity forms input labels instead of value

On a WordPress > Woocommerce cart page, I'm using product variables. Instead of the values of the variations, I would like to display the labels.

This are the values:

["_gravity_form_lead"]=>
  array(9) {
    [3]=>
    string(1) "6"
    [2]=>
    string(1) "6"
    [5]=>
    string(1) "1"
    [6]=>
    string(4) "1.25"
    [7]=>
    string(1) "0"
    [8]=>
    string(4) "2.85"
    ["1.1"]=>
    string(11) "Total Price"
    ["1.2"]=>
    string(6) "$10.24"
    ["1.3"]=>
    string(1) "1"
  }

But for example instead of [5]=>string(1) "1" which is 1, I should have the label of the variation called "Single Sided".

Is there any function that can help me list each every product variation separated and not in group, so I would have total control over them what kind of details to list regarding a variation?

Upvotes: 2

Views: 5182

Answers (2)

user621639
user621639

Reputation:

Based on the above answer, I figured out what I needed. Hope that will help also others. Thanks a lot for Peter van der Does!

// get form fields individually
echo '<dl class="variation">';
foreach ($woocommerce->cart->cart_contents as $cart_key => $cart_item_array) {
    $form = GFFormsModel::get_form_meta($cart_item_array['_gravity_form_data']['id']);
    // height
    $height = GFFormsModel::get_field($form, '3');
    foreach($height['choices'] as $choice) {
        if($choice['value'] == $cart_item_array['_gravity_form_lead']['3']) {
            echo '<dt>Height:</dt> <dd>'.$choice['text'].'</dd>';
        }
    }
    // width
    $width = GFFormsModel::get_field($form, '2');
    foreach($width['choices'] as $choice) {
        if($choice['value'] == $cart_item_array['_gravity_form_lead']['2']) {
            echo '<dt>Height:</dt> <dd>'.$choice['text'].'</dd>';
        }
    }
    // printing
    $printing = GFFormsModel::get_field($form, '5');
    foreach($printing['choices'] as $choice) {
        if($choice['value'] == $cart_item_array['_gravity_form_lead']['5']) {
            echo '<dt>Printing:</dt> <dd>'.$choice['text'].'</dd>';
        }
    }
    // lamination
    $lamination = GFFormsModel::get_field($form, '6');
    foreach($lamination['choices'] as $choice) {
        if($choice['value'] == $cart_item_array['_gravity_form_lead']['6']) {
            echo '<dt>Lamination:</dt> <dd>'.$choice['text'].'</dd>';
        }
    }
    // quantity
    $quantity = GFFormsModel::get_field($form, '8');
    foreach($quantity['choices'] as $choice) {
        if($choice['value'] == $cart_item_array['_gravity_form_lead']['8']) {
            echo '<dt>Quantity:</dt> <dd>'.$choice['text'].'</dd>';
        }
    }
}
echo '</dl>';

Upvotes: 0

Peter van der Does
Peter van der Does

Reputation: 14468

$form_id = RGFormsModel::get_form_id('Form name'); // replace Form name with your form name
$form = GFFormsModel::get_form_meta($form_id);
$field = GFFormsModel::get_field($form, ##); // ## Is the id, so 5 or "1.3"
if(is_array(rgar($field, "inputs"))){ // For the "1.1" etc ID's
    foreach($field["inputs"] as $input){
        if ( $input['id'] == "##" ) { // ## Is the id, so "1.1", "1.2" etc..
            $label = $input['label'];
        }
    }
} else {
    $label = GFFormsModel::get_label($field);
}

In case of the ID's "1.1", "1.2", "1.3", which are grouped ID's, the get_label will return the group name, like when you have a Name and a First name, Last Name. it will return Name. The check for the is_array will give you label names like "First name" for example.

Code wise this should be improved, like looping through the fields you have and doing the above code, but I'm assuming you would know how to code this,

Upvotes: 3

Related Questions