将 Gravity Forms 中的字段连接到 WooCommerce 中的字段

Connect a field in Gravity Forms to a field in WooCommerce

我想构建一个 Gravity 表单,用户可以在其中通过 Ajax 搜索来搜索所有产品。该搜索框必须连接到 WooCommerce 中的 "Product title" 字段。

因此,当用户在 Gravity 表单搜索框中键入第一个字母(例如:PIN)时,Ajax 下拉列表会显示数据库中可用的 2 本书名:"PINNOCHIO" 和 "PINNOCHIO RETURNS",例如。然后用户选择他感兴趣的标题,然后提交表单。

我不知道如何将 Gravity 中的字段连接到 WooCommerce 中的字段。可以吗?

您需要添加一些代码来动态填充下拉菜单。这是它的官方文档:https://docs.gravityforms.com/dynamically-populating-drop-down-fields

如果我采用他们的起点并重新配置它以使用产品,它看起来像这样:

add_filter( 'gform_pre_render_51', 'populate_posts' );
add_filter( 'gform_pre_validation_51', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_51', 'populate_posts' );
add_filter( 'gform_admin_pre_render_51', 'populate_posts' );
function populate_posts( $form ) {

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-products' ) === false ) {
            continue;
        }

        $posts = get_posts( array(
            'post_type' => 'product',
            'numberposts' => -1,
        ) );

        $choices = array();

        foreach ( $posts as $post ) {
            $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
        }

        // update 'Select a Product' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select a Product';
        $field->choices = $choices;

    }

    return $form;
}