第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WordPress:使用 Gravity Forms 中的 WooCommerce 產(chǎn)品填充列表字段

WordPress:使用 Gravity Forms 中的 WooCommerce 產(chǎn)品填充列表字段

PHP
元芳怎么了 2022-12-11 16:12:53
我想讓用戶在一個表單中選擇產(chǎn)品。為此,我需要使用已發(fā)布的 WooCommerce 產(chǎn)品動態(tài)填充表單。我找到了一個用 WooCommerce 產(chǎn)品填充選擇字段的解決方案。但是用戶應該能夠選擇每種產(chǎn)品的數(shù)量。因此我需要使用列表字段并填充列表字段的單元格。以下是列表字段的外觀:SKU 和標題字段應動態(tài)填充。數(shù)量字段供用戶填寫。這是我使用 WooCommerce 產(chǎn)品數(shù)據(jù)填充選擇字段的代碼: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;}
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經(jīng)驗 獲得超2個贊

只需稍微修改您的代碼,將第一列作為選擇字段,并動態(tài)填充產(chǎn)品名稱,就不會顯示很長的列表。


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

}


查看完整回答
反對 回復 2022-12-11
?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超21個贊

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


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


}



查看完整回答
反對 回復 2022-12-11
  • 2 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號