我们有4个重力形式。
1. Input an Australian postcode
2. Select swimming pool dimensions
3. Select accessories
4. Show quotation, prompt for contact details.
在functions.php中,我们定义了一些全局符号。 $PostCode
设置为第1页上输入的邮政编码值。一旦$PostCode
为> 0我们包含一个php文件。此文件包含由邮政编码索引的大量运费。特定于邮政编码的费用将转移到表单第2页的隐藏字段,并用于以后的计算。
随着运费是一个公式。此公式使用第2页上的隐藏字段中的数据以及第2页上的数字字段进行评估,该字段根据长度和宽度的输入字段计算池区域。
评估公式的结果应输入第3页的字段,以便用于其他计算。此时遇到问题,即接收隐藏字段未更新。
代码:
// global symbols
$Formula1 = '';
$TNTFreightPrices = array();
$FieldNumberContent = array();
$PostCode = '';
$Formula1Result = 0;
add_filter('gform_field_input_11', 'update_hidden', 10, 5);
function update_hidden($input, $field, $value, $lead_id, $form_id)
{
global $TNTFreightPrices, $FieldNumberContent, $Formula1, $PostCode, $Formula1Result;
$pgno = $field['pageNumber'];
$PostCode = rgpost("input_3");
if (!empty($PostCode)) {
$path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/tntfreightprices/TNTFreightPrices.php';
$tmp = include_once $path;
if (gettype($tmp) == "array") {
$TNTFreightPrices = $tmp['postcodes'];
$Formula1 = $tmp['Formula1'];
$f = $form_id;
$BasicChrg = $TNTFreightPrices[$PostCode]['BasicChrg'];
$KgChrg = $TNTFreightPrices[$PostCode]['KgChrg'];
$MinChrg = $TNTFreightPrices[$PostCode]['MinChrg'];
$RemotAreaChrg = $TNTFreightPrices[$PostCode]['RemotAreaChrg'];
$ResidChrg = $TNTFreightPrices[$PostCode]['ResidChrg'];
$_0to15Chrg = $TNTFreightPrices[$PostCode]['0to15Chrg'];
$_15to199Chrg = $TNTFreightPrices[$PostCode]['15to199Chrg'];
$_200to299Chrg = $TNTFreightPrices[$PostCode]['200to299Chrg'];
$_300to399Chrg = $TNTFreightPrices[$PostCode]['300to399Chrg'];
$_400to499Chrg = $TNTFreightPrices[$PostCode]['400to499Chrg'];
$_500to599Chrg = $TNTFreightPrices[$PostCode]['500to599Chrg'];
$_600plusChrg = $TNTFreightPrices[$PostCode]['600plusChrg'];
$FuelSurchrg = $TNTFreightPrices[$PostCode]['FuelSurchrg'];
$FieldNumberContent = array(
'85' => Content_tag(
"input", "",
array(
"id" => "input_{$f}_85",
"class" => "gform_hidden",
"name" => "input_85",
"aria-invalid" => "false",
"value" => "$BasicChrg",
"type" => "hidden")
),
// ...
未显示Content_tag
功能。它生成HTML以设置第2页上隐藏字段的值。其输出的一个示例是
input id="input_11_85" class="gform_hidden" name="input_85" aria-invalid="false" value="9.33" type="hidden">
这些值存储在$FieldNumberContent
数组中,并根据field_id返回给调用者,即
$fid = $field['id'];
if (array_key_exists($fid, $FieldNumberContent)) {
$input = $FieldNumberContent[$fid];
}
update_hidden
函数以
return $input;
}
下一个代码块采用第2页上的隐藏字段的值和区域计算的结果,也在第2页上,并替换$Formula1
中存储的公式中的匹配值,最后进行评估字符串并将其结果存储在全局$Formula1Result
中。
add_filter('gform_field_value', 'populate_fields_into_formula1', 10, 3);
function populate_fields_into_formula1( $value, $field, $name )
{
global $Formula1, $PostCode, $TNTFreightPrices, $Formula1Result;
if (array_key_exists($PostCode, $TNTFreightPrices) ) {
$fid = $field['id'];
if (strpos($Formula1, "[") !== false) {
$sym = "[{$field['label']}]";
$val = rgpost("input_{$fid}");
$Formula1 = str_replace($sym, $val, $Formula1);
} else {
$f1 = "return {$Formula1};";
$f1 = str_replace(",", "", $f1);
$Formula1Result = eval($f1);
}
}
return $value;
}
示例公式是
[AreaCalculator]/2 * [KgChrg] >= [MinChrg] ? [AreaCalculator]/2 * [KgChrg] : [MinChrg]
最后,这是应该更新第3页的字段隐藏字段的代码。我们点击了复选框,允许动态更新字段并将参数名称设为formula1result
。
add_filter('gform_field_value_formula1result', 'populate_formula1result', 10, 3);
function populate_formula1result($value, $field, $name)
{
global $Formula1Result;
if ($Formula1Result > 0) {
$value = $Formula1Result;
}
return $value;
}
现在我承认我不是一个特别熟练的PHP程序员。也许有更好的方法来做这一切,相信我,我都是耳朵。 但是我一直在用这种方式对抗这种情况持续一天或更长时间。其他一切似乎都在起作用。只有这不是为什么?