【问题标题】:WordPress: Populate list field with WooCommerce products in Gravity FormsWordPress:使用重力形式的 WooCommerce 产品填充列表字段
【发布时间】:2020-07-20 01:19:45
【问题描述】:

我想让用户在表单中选择产品。 为此,我需要使用已发布的 WooCommerce 产品动态填充表单。

我找到了使用 WooCommerce 产品填充选择字段的解决方案。 但是用户应该能够选择每个产品的数量。因此我需要使用列表字段并填充列表字段的单元格。

列表字段的外观如下:

应动态填充 SKU 和标题字段。 数量字段供用户填写。

这是我用 WooCommerce 产品数据填充选择字段的代码:

add_filter( 'gform_pre_render_5', 'populate_posts' );
add_filter( 'gform_pre_validation_5', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_5', 'populate_posts' );
add_filter( 'gform_admin_pre_render_5', 'populate_posts' );
function populate_posts( $form ) {

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

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

        // you can add additional parameters here to alter the posts that are retrieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts
        $posts = get_posts( 'numberposts=-1&post_status=publish&post_type=product' );

        $choices = array();

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

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

    }

    return $form;
}

不幸的是,我不知道如何动态填充列表字段的单元格。

我找到了一个如何填充列表字段的示例:https://docs.gravityforms.com/gform_field_value_parameter_name/#list-field

但我不知道如何将这两个 sn-ps 结合起来:/

add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
  $list_array = array(
            array(
                'Column 1' => 'row1col1',
                'Column 2' => 'row1col2',
                'Column 3' => 'row1col3',
            ),
            array(
                'Column 1' => 'row2col1',
                'Column 2' => 'row2col2',
                'Column 3' => 'row2col3'
            ),
  );
    return $list_array;
}

【问题讨论】:

    标签: php wordpress woocommerce gravity-forms-plugin gravityforms


    【解决方案1】:

    如果有人感兴趣,这是我的工作代码。

    这里有一些很大的帮助:https://stackoverflow.com/a/61098114/1788961

    add_filter( 'gform_field_value_list', 'populate_list' );
    function populate_list( $value ) {
    
        global $current_user;
        get_currentuserinfo();
    
        $statuses = array('publish', 'draft');
    
        // Args on the main query for WC_Product_Query
        $args = [
            'status'    => $statuses,
            'orderby'   => 'name',
            'order'     => 'ASC',
            'limit'     => -1,
        ];
    
        $vendor_products = wc_get_products($args);
    
        $list_array = array();
    
        foreach ($vendor_products as $key => $product) {
    
            if ($product->get_type() == "variable") {
    
                // Args on product variations query for a variable product using a WP_Query
                $args2 = array( 
                    'post_parent' => $product->get_id(), 
                    'post_type'   => 'product_variation', 
                    'orderby'     => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ), 
                    'fields'      => 'ids', 
                    'post_status' => $statuses, 
                    'numberposts' => -1, 
                ); 
    
                foreach ( get_posts( $args2 ) as $child_id ) {
                    // get an instance of the WC_Variation_product Object
                    $variation = wc_get_product( $child_id ); 
    
                    if ( ! $variation || ! $variation->exists() ) {
                        continue;
                    }
    
                    $list_array[] = array(
                        'SKU'      => $variation->get_sku(),
                        'Name'     => $product->get_name() . " - " . $child_id,
                    );
                }
    
            } else {
    
                $list_array[] = array(
                    'SKU'      => $product->get_sku(),
                    'Name'     => $product->get_name(),
                );
    
            }
    
        }
    
        return $list_array;
    
    }
    

    【讨论】:

    • 好东西。你的用例是什么?
    • 我想让产品供应商(WC Vendors)通过表单添加一些关于他们产品的信息。因此我只需要在表格中显示他们的产品。
    猜你喜欢
    • 2017-12-23
    • 2021-06-05
    • 2016-02-19
    • 1970-01-01
    • 2019-11-01
    • 2014-11-16
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多