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

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;
}
- 2 回答
- 0 關注
- 131 瀏覽
添加回答
舉報