簡體   English   中英

WordPress:在 Gravity 中使用 WooCommerce 產品填充列表字段 Forms

[英]WordPress: Populate list field with WooCommerce products in Gravity Forms

我想讓用戶 select 產品在一個表格中。 為此,我需要使用已發布的 WooCommerce 產品動態填充表單。

我找到了一種使用 WooCommerce 產品填充 select 字段的解決方案。 但用戶應該能夠 select 每個產品的數量。 因此我需要使用列表字段並填充列表字段的單元格。

以下是列表字段的外觀:

在此處輸入圖像描述

SKU 和標題字段應動態填充。 數量字段供用戶填寫。

這是我用 WooCommerce 產品數據填充 select 字段的代碼:

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

但我不知道如何組合這兩個片段:/

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

如果有人感興趣,這是我的工作代碼。

從這里得到一些很大的幫助: 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;

}

只需稍微修改您的代碼以將第一列設為 select 字段並動態填充產品名稱以不顯示長列表。

add_filter( 'gform_column_input_4_11_1', 'set_column', 10, 5 );
function set_column( $input_info, $field, $column, $value, $form_id ) {
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);

$choices = 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;
            }
            

            $choices[] = array( 'text' => $product->get_name() . " - " . $child_id, 'value' => $product->get_name() . " - " . $child_id );
        }

    } else {
        $choices[] = array( 'text' => $product->get_name(), 'value' => $product->get_name() );


    }

}

return array( 'type' => 'select', 'choices' => $choices );

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM