Skip to content

Commit

Permalink
gw-calculated-shipping: Adding support for User Defined Shipping fi…
Browse files Browse the repository at this point in the history
…elds. Fixed issue where Calculated Shipping was not reflected in order summary on Entry Detail.
  • Loading branch information
spivurno committed Jul 4, 2023
1 parent 8819d8a commit b53cbb1
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions gravity-forms/gw-calculated-shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*
* Instruction Video: https://www.loom.com/share/aa10e3aceb4247528a27d1b27cc50516
*
* A simple method for using a calculated product field as a shipping field. This provides the ability to use
* calculations when determining a shipping price.
* A simple method for using a Calculated Product field (or User Defined Price field) as a shipping field. This provides
* the ability to use calculations when determining a shipping price.
*
* Note: This snippet does not work with GP eCommerce Fields.
*
* Plugin Name: Gravity Forms - Calculated Shipping
* Plugin URI: https://gravitywiz.com/
* Description: Use a calculated product field as a shipping field.
* Author: Gravity Wiz
* Version: 1.1
* Version: 1.2
* Author URI: https://gravitywiz.com/
*/
class GWCalculatedShipping {
Expand All @@ -26,7 +28,7 @@ function __construct( $args ) {
'field_id' => false,
) );

add_filter( 'gform_pre_validation', array( $this, 'add_shipping_field' ), 9 );
add_filter( 'gform_pre_process', array( $this, 'add_shipping_field' ), 9 );
add_filter( 'gform_pre_render', array( $this, 'restore_original_field' ), 11 );

}
Expand All @@ -37,10 +39,20 @@ function add_shipping_field( $form ) {
return $form;
}

// Trigger generation of admin version of product info that is typically only generated when accessing via Entry Detail
// so it has our dynamically added Shipping field.
add_filter( 'gform_product_info', array( $this, 'trigger_admin_product_info' ), 10, 3 );

// get our calc shipping field and convert it to default shipping field
// REMINDER: PHP objects are always passed by reference
$field = GFFormsModel::get_field( $form, $this->_args['field_id'] );

if ( $field->get_input_type() === 'calculation' ) {
$shipping_value = rgpost( "input_{$field->id}_2" );
} else {
$shipping_value = rgpost( "input_{$field->id}" );
}

// create a copy of the original field so that it can be restored if there is a validation error
$this->_orig_field = GF_Fields::create( $field );

Expand All @@ -51,7 +63,7 @@ function add_shipping_field( $form ) {
$field = GF_Fields::create( $field );

// map calc value as shipping value
$_POST[ "input_{$field->id}" ] = rgpost( "input_{$field->id}_2" );
$_POST[ "input_{$field->id}" ] = $shipping_value;

foreach ( $form['fields'] as &$_field ) {
if ( $_field->id == $field->id ) {
Expand All @@ -62,6 +74,19 @@ function add_shipping_field( $form ) {
return $form;
}

function trigger_admin_product_info( $product_info, $form, $entry ) {

if ( $this->_args['form_id'] != $form['id'] ) {
return $form;
}

remove_filter( 'gform_product_info', array( $this, 'trigger_admin_product_info' ) );

GFCommon::get_product_fields( $form, $entry, false, true );

return $product_info;
}

function field( $args = array() ) {
return wp_parse_args( $args, array(
'id' => false,
Expand Down

0 comments on commit b53cbb1

Please sign in to comment.