This walk-through will demonstrate how to calculate and add tax to your entry total. There are two pieces to the Gravity Forms pricing puzzle (easy puzzle, huh?):
- the total that is calculated on the front-end as the user selects various products
- the total that is calculated (and validated!) in the backend
Any time you are customizing Gravity Form pricing, you'll need to make sure you implement your changes in both places.
The Front-end Total
To modify the front-end total we're going to use a Gravity Form definable function: gform_product_total. Gravity Forms will automatically check for this function and if you've defined it, it will run every time the total is updated. Here is snippet of code that defines this function for our form:
// update the "78" to the ID of your form
add_filter('gform_pre_render_78', 'add_total_script');
function add_total_script($form) {
?>
<script type="text/javascript">
function gform_product_total(formId, total){
var tax = ((20 * total) / 100); // update the "20" to the desired tax percentage;
return total + tax;
}
</script>
<?php
return $form;
}
You'll notice that we're using the gform_pre_render hook to output our script block to the page. There are multiple (possibly better) ways to accomplish this, but for those of you with limited coding knowledge, this is likely your easiest option.
Inside the script block we define our gform_product_total function. Our first task is calculating the tax. In our case, we're adding a 20% tax. If you are also doing a percentage tax, just update the "20" to the tax percentage your situation requires. If your tax requirements are different, feel free to use any equation to determine the tax.
Now that we have our tax, we need to return the total plus the tax to Gravity Forms. GF will handle updating this modified total to the total field (if you have the total field on your form).
The Back-end Total
Next up. The back-end total. Remember, customizations made to pricing fields on the front-end almost always need to be supported in the back-end. This means more custom code. So on the front-end we've updated our total with the 20% tax. Now let's make sure it gets added in the back-end as well.
// update the "78" to the ID of your form
add_filter('gform_product_info_78', 'update_product_info', 10, 3);
function update_product_info($product_info, $form, $entry) {
$total = get_base_order_total($product_info['products']);
$tax = (20 * $total) / 100; // update the "20" to the desired tax percentage
$product_info['products']['tax'] = array(
'name' => 'Tax', // name that will appear in PayPal and pricing summary tables
'price' => $tax, // amount of total tax
'quantity' => 1
);
return $product_info;
}
function get_base_order_total($products) {
$total = 0;
foreach($products as $product) {
extract($product);
$total += GFCommon::to_number($price) * $quantity;
}
return $total;
}
Hm, that looks like a bit more code than we needed for the front-end, huh? Don't worry. It's just as easy. The first step is to hook into the gform_product_info hook. This hook will pass whatever function we tie to it an array of all the form's product information, including how many of each item were selected on the current order. With this in mind, here's a step by step of how we're going to use that product information.
$total = get_base_order_total($product_info['products']);
We're using the get_base_order_total() custom function to retrieve the total of the current products. You'll see where we've defined this function at the bottom of the snippet. It's important to note that this is note an official Gravity Forms function; just a custom helper function that aids us in this scenario. This function will return the total based on the selected products and their base prices.
$tax = (20 * $total) / 100; // update the "20" to the desired tax percentage
Now that we have the total we can calculate the tax. Again, if you are also using a percentage tax, just update the "20". If not, update the equation to get the tax result you require.
$product_info['products']['tax'] = array(
'name' => 'Tax', // name that will appear in PayPal and pricing summary tables
'price' => $tax, // amount of total tax
'quantity' => 1
);
Now, remember how the gform_product_info hook passes all the form product information in the $product_info array? Well, we can actually customize the product information in this array before returning it!
In the above snippet, we're simply adding a new product titled "Tax" and setting the price to the calculated tax price we determined in the previous step. Since we calculated our tax on the order total, we can set the quantity to "1" so the Tax product is only applied once. Now we just return the modified $product_info array and Gravity Forms takes care of the rest.
Summary
Now that we have our front-end and back-end modifications in place, anytime a new entry is submitted, Gravity Forms will automatically handle displaying the adjusted total to the user on the front-end, sending that adjust total on to PayPal (if you're using the Gravity Forms PayPal Add-on with this form) and record the adjusted total as part of entry!