index.php

Gravity Forms Dynamic Price Modifier

This WordPress code snippet for Gravity Forms allows dynamic modification of product field prices based on form navigation. When a user reaches the second page of a multi-page form, it updates the price of a designated product field. This is useful for forms where pricing needs to adjust based on user input across different form pages.

<?php /** * Modify the value of a price field between pages in a multi-page Gravity Forms form. * * @param array $form The Gravity Forms form array. * @return array The modified form array. */ add_action('gform_pre_render', 'cwpai_modify_price_field'); add_action('gform_pre_validation', 'cwpai_modify_price_field'); function cwpai_modify_price_field($form) { // Define the form ID and price field ID $target_form_id = 1; // Replace with your form ID $price_field_id = 2; // Replace with your price field ID // Check if the current form is the target form if ($form['id'] != $target_form_id) { return $form; } // Get the current page of the form $current_page = GFFormDisplay::get_current_page($form['id']); // Check if the current page is the desired page for modification (e.g., page 2) if ($current_page == 2) { // Loop through the form fields foreach ($form['fields'] as &$field) { // Check if the field is a product field and matches the price field ID if ($field->type == 'product' && $field->id == $price_field_id) { // Modify the base price of the field $field->basePrice = 1234; // Set your desired price value here break; } } } return $form; }

Frequently Asked Questions

The function hooks into Gravity Forms to modify the price of a product field when a user navigates to a specific page within a multi-page form.