2

在 WordPress > Woocommerce 购物车页面上,我正在使用产品变量。我想显示标签,而不是变化的值。

这是值:

["_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"
  }

但例如,我应该将变体的标签称为“ ”,而不是[5]=>string(1) "1"is 。1Single Sided

是否有任何功能可以帮助我单独列出每个产品变体而不是分组,所以我可以完全控制它们列出关于变体的哪些细节?

4

2 回答 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);
}

对于 ID 的“1.1”、“1.2”、“1.3”,它们是分组 ID,get_label 将返回组名,就像你有一个名字和一个名字、姓氏一样。它将返回名称。对 is_array 的检查将为您提供标签名称,例如“名字”。

代码方面,这应该得到改进,比如遍历你拥有的字段并执行上面的代码,但我假设你会知道如何编写代码,

于 2013-07-16T14:14:01.927 回答
0

根据上面的答案,我想出了我需要什么。希望这对其他人也有帮助。非常感谢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>';
于 2013-07-16T19:49:08.873 回答