在为此苦苦挣扎了一个多小时之后,似乎没有任何方法可以直接从 Gravity Forms 获得它。你可能不再需要它了,但也许它会节省别人的时间。
在我的例子中,我需要一些方法来获取这个值并在提交条目之前使用它进行验证,所以我使用了钩子gform_validation
你最好的选择,也是我最终决定的,是使用 Gravity Forms 返回的值来找到你想要的文本
add_filter('gform_validation', 'price_validation', 10, 2);
function price_validation($validation_result, $context){
$form = $validation_result['form'];
foreach ($form['fields'] as $field ){
//Find our product field.
if ($field->type == 'product'){
//Explode useless Gravity Forms string into array.
$value = explode('|', rgpost("input_{$field->id}"));
//Get the product choices.
$choices = $field->choices;
//Find the key of the chosen option.
$key = array_search($value[0], array_column($choices, 'value'));
//Get the text of this option.
$text = $choices[$key]['text'];
//Make array with all values obtained.
$value = array(
'text' => $text,
'value' => $value[0],
'price' => number_format($value[1], 2, ',', ' '),
);
//Use $value for whatever you want.
}
}
$validation_result['form'] = $form;
return $validation_result;
}
或者,如果您只需要价格:
$value = rgpost("input_{$field->id}");
if (($pos = strpos($value, "|")) !== false) {
$price = number_format(substr($value, $pos + 1), 2, ',', ' ');
}