Let’s say we have an orders table. When a user makes changes to a record and saves them, we want to automatically calculate the value of the total field using the fields subtotal, discount and sales_tax, where discount and sales_tax are stored as percentages (i.e. a discount value of 10 means 10% of subtotal):
function
tablename_before_update(&
$data
,
$memberInfo
, &
$args
){
// calculate total after applying discount
$data
[
'total'
] =
$data
[
'subtotal'
] * (1 -
$data
[
'discount'
] / 100);
// calculate total after applying sales tax
$data
[
'total'
] =
$data
[
'total'
] * (1 +
$data
[
'sales_tax'
] / 100);
return
true;
}