內容簡介
Advanced Custom Fields的一個外掛,可添加公式欄位,可根據欄位值進行簡單的數學運算。在編輯後端文章編輯器以及前端表單上通過ajax動態更新計算。
要求
Calculated fields需要Advanced Custom Fields 5.0或更高版本,並與Pro版本以及標準版本兼容。
前端表單
從1.2.3版本開始,Calculated fields for ACF支持使用acf_form()函數(內置於Advanced Custom Fields)創建的前端表單。
公式
Calculated fields為ACF字段編輯器添加了一個新的設置“Formula”。公式支持使用字段名引用同一字段組中的其他字段。如果在重複字段內添加公式,則字段名引用同一重複字段內的另一個字段。
重複字段內的子字段還可以使用“parent. ”前綴引用父字段。
公式可以包含基本的數學運算:加(+)、減(-)、乘(*)、除(/)和次方(^)。公式還可以包含像 sin、cos、arcsin、log、ln、sqrt、abs 等函數。支持括號。
如果由於無效的語法或參考未定義的字段名而無法評估表達式,它將悄悄地返回零。
有效表達式示例(注意,開頭沒有等號):
fieldA * 2
fieldA * fieldB
abs(fieldA) * (2 + sqrt(fieldB))
amount * parent.price
條件運算符
表達式還可以包含條件式,將返回0或1。支持的運算符有:
等於 (==) - 當兩個值相等時返回1,否則返回0
不等於 (! =) - 當兩個值不相等時返回1,否則返回0
大於 (>) - 當第一個操作數大於第二個時返回1,否則返回0
大於等於 (> =) - 當第一個操作數大於或等於第二個時返回1,否則返回0
小於 (<) - 當第一個操作數小於第二個時返回1,否則返回0
小於等於 (< =) - 當第一個操作數小於或等於第二個時返回1,否則返回0
使用條件運算符的有效表達式示例:
10 == 10(返回1)
10 == 2(返回0)
10 > 2(返回1)
10 < 2(返回0)
10 <= 10(返回1)
四捨五入函數
將十進制值四捨五入到最近的整數。支持的函數有:
round() - 使用標準數學捨入規則四捨五入到最近的整數
ceil() - 將值捨入到下一個更高的整數
floor() - 將值捨入到下一個更低的整數
使用四捨五入函數的有效表達式示例:
round(10.2)(返回10)
round(10.9)(返回11)
round(10.888888 * 10) / 10(返回10.9)
ceil(10.2)(返回11)
floor(10.2)(返回10)
請注意,round() 函數只接受一個參數,並始終捨入為整數。如果您需要捨入到更高的精度,請像上面的第三個示例一樣進行乘法和除法。
群組字段
從1.2.4版本開始加入了群組字段(subfields),可以使用組名作為前綴並且可以使用“parent”前綴在該組內引用parent level的字段。請參閱
外掛標籤
開發者團隊
原文外掛簡介
Adds a formula fields ot Advanced Custom Fields that allows you to perform simple math based on field values. Calculations are updated dynamically via ajax while editing a post on the backend post editor as well as on front end forms.
Requirements
Calculated fields requires Advanced Custom Fields 5.0 or later and works with Pro as well as the standard version.
Frontend forms
Beginning in version 1.2.3, Calculated fields for ACF supports frontend forms created using the acf_form() function (built in to Advanced Custom Fields).
Formulas
Calculated fields adds a new setting “Formula” to the the ACF field editor. Formulas supports referring to other fields in the same Field group using the field name. If a formula is added to a sub field inside a repeater field, the name refers to another sub field in the same repeater.
A sub field inside a repeater field can also refer to a parent field using the “parent.”” prefix.
A formula can contain the basic mathematical operations: plus (+), minus (-), multiply (*), division (/) and power of (^). Formulas can also contains functions like sin, cos, arcsin, log, ln, sqrt, abs. Parentheses are supported.
If an expression can’t be evaluated due to invalid syntax or referring to undefined field names, it will silently return zero.
Examples of valid expressions (note, no equal sign at the beginning):
fieldA * 2
fieldA * fieldB
abs(fieldA) * (2 + sqrt(fieldB))
amount * parent.price
Conditional operators
An expression can also contain a conditional expression that will return either 0 or 1. The supported operators are:
Equals (==) – Returns 1 when two values are equal, 0 otherwise
Not equals (!=) – Returns 1 when two values are not equal, 0 otherwise
Greater than (>) – Returns 1 when the first operand is greater than the second, 0 otherwise
Greater than or equal (>=) – Returns 1 when the first operand is greater than or equal to the second, 0 otherwise
Less than (<) – Returns 1 when the first operand is less than the second, 0 otherwise
Less than or equal (<=) – Returns 1 when the first operand is less than or equal to the second, 0 otherwise
Examples of valid expressions using conditional operators:
10 == 10 (returns 1)
10 == 2 (returns 0)
10 > 2 (returns 1)
10 < 2 (returns 0)
10 <= 10 (returns 1)
Rounding functions
Round a decimal value to the nearest integer. Supported functions are:
round() – Uses standard mathematical rounding rules to round to nearest integer
ceil() – Rounds the value to the next higher integer
floor() – Rounds the value to the next lower integer
Examples of valid expressions using rounding functions:
round(10.2) (returns 10)
round(10.9) (returns 11)
round(10.888888 * 10) / 10 (returns 10.9)
ceil(10.2) (returns 11)
floor(10.2) (returns 10)
Note that the round() function only takes one parameter and always round to an integer. If you need to round to a higher precision, multiply and divide as shown in third example above.
Group fields (new from 1.2.4)
Fields defined as part of a group, subfields, can be addressed using the group name as a prefix. Inside the group, a field defined on the parent level can be addressed using the “parent” prefix. See examples below:
Valid formula to use the value of field “foobar” defined inside the group “group2”:
* group2.foobar + 10
Valid formula in a field inside a group (assuming the parent has a field named count):
* parent.count * 22
Array functions for repeater fields
If repeater fields are used, a calculated field in the parent field group can summarize a specific repeater sub fields using the aggregation functions sum, count, average, min or max. For example, the expression: “Sum(orderlines.amount)”” will return the sum of all the “amount” fields in the repeater field “orderlines”.
The available array functions are:
sum()
average()
count()
min()
max()
Note that anything after the dot (.) in the aggregate expression is an expression in itself. It’s perfectly OK to write a formula like: “Sum(orderlines.price * amount)”. This expression will walk over all lines in the “orderlines” repeater field, perform the calculation “price * amount” and return the sum of all lines.
Note that when working with aggregate functions, parentheses can not be used. If you need to aggregate a more complex calculation, you should add an extra field in the repeater group and let the aggregate function work on this extra field.
Data type and calculation order
Calculated fields works with the assumption that all fields are defined as numeric in the ACF editor. Using a text field in a formula WILL PRODUCE UNPREDICTABLE RESULTS.
Calculations are made as the custom field is stored to the database. To minimize the impact of performance, Calculated fields rely on field order. A formula field can only refer to fields ordered BEFORE itself. A formula field that refers to a field ordered after it WILL PRODUCE UNPREDICTABLE RESULTS.
