2

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?

2
  • Is the [5] and ["1.1"] the field id's on the Form? Commented Jul 16, 2013 at 3:11
  • 5 is the id, and 1 is the value. I need to get the label of the value. Commented Jul 16, 2013 at 7:22

2 Answers 2

3
$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,

Sign up to request clarification or add additional context in comments.

2 Comments

Peter van der Does, thank you for your help. Very nice approach, but it is returning all fields (with labels of course, thank you) added via wp-admin. I need only the labels of values selected by user for the added product in cart.
Oh, I got the point. Excellent suggestion. Thank you very much! I will post below also how I did modified your answer. Accepted. I think the two entries will help also others.
0

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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.