Gravityforms - 添加类别中间单选按钮字段

时间:2021-04-25 22:06:39

标签: radio-button categories display gravityforms

我正在使用模板中的functions.php 填充Gravityforms 字段,它的作用就像一个魅力,但我有一个字段是……一个挑战。我已经能够很好地填充我的数据库中的选项,但是使用functions.php 我无法控制字段显示区域的内容,例如,我可以为每个类别添加标题或标题。有没有办法以编程方式调整显示,这是我希望完成的示例

RadioButton Choice (Field ID 21)
    Dark Colors (category title)
        maroon (choice 1)
        navy blue (choice 2)
        black (Choice 3)
    Standard Colors (Category Title)
        Red (choice 4)
        blue (choice 5)
        gray (Choice 6)
    Light Colors )Category Title)
        pink (choice 7)
        sky blue (choice 8)
        white (Choice 9)

我只是在寻找一种在选项之间添加类别标题的方法。我的数据库查询将类别作为响应的一部分,但我必须填充选项以提供数组的唯一选项。

我已经看到我可以在哪里添加额外的 Gravityform 字段并让它们由相同的“单选”单选按钮选项控制,但是涉及的类别根据我调用以动态填充选择的数据库查询而改变,范围可以从 1 category 到 10,它不会在表单本身中包含相关字段,因为这些都在一个单选按钮字段下。

如有任何想法,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够使用我在评论中发布的链接来提供解决方案。作为我“选择”的一部分

这是预渲染填充选项的功能。在这种情况下,我使用产品图片而不是默认单选按钮填充单选按钮字段

add_filter('gform_pre_render_1', 'populate_choices');
function populate_choices($form) {
    global $wpdb;

    // Now to get a list of the products I want to include on this radio-button
    $dataQ = "
        SELECT category, product_id, product_name 
        FROM product_table
        WHERE product_type = 'whatever I am looking for'
        ORDER BY category, product_name ASC
    ";
    $dataR = $wpdb->get_results($dataQ);
    $dataList = array();

    // get current protocol to use in Product Images
    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";

    // Rebuild URL for use with product image graphics
    $productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';

    // Generate list of choices for the radio-button, along with category headers
    foreach ($form['fields'] as $field) {
        if ($field->id == '32') { // 32 My specific radio-button field
            $category = "";
            foreach ($dataR as $data) {
                if ($category != $data->category) {
                    // If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
                    $category = $data->category;
                    $dataList[] = array(
                        'text' => '#'.$category."#",
                        'value' => ''
                    );
                }
                $productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
                $productID = $data->product_id;
                $dataList[] = array(
                    'text' => $data->product_name,
                    'value' => $productID,
                    'isSelected' => false,
                    'price' => '0.00',
                    'imageChoices_image' => $productImagehUrl.$productImage
                );
            }
            $field->choices = $dataList;
        }
    }
}

然后我添加了一个特定的字段修饰符,用 html 更新“#category#”选择元素,使类别名称显示出来。

// numbers after filter = "1" for form ID 1, "32" for field ID 32
add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
    if ( strpos( $choice['text'], '#' ) === 0 ) {
        $categoryName = str_replace("#","",$choice['text']);
        $choice_markup = '
        <li style="width:100%;text-align:left;">
            <span style="color:#000000;font-weight:bold;font-size:18px;">
                <br>
                '.$categoryName.'
            </span>
        </li>
       ';
    }
    return $choice_markup;
}, 10, 2);
相关问题