Forked from spivurno/gw-gravity-forms-calculated-shipping.php
Created
April 29, 2020 04:05
-
-
Save claygriffiths/d30e07a7e6d1b9474ac0059861982b6d to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Calculated Shipping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Calculated Shipping | |
* | |
* 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. | |
* | |
* @version 1.2 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
* @copyright 2016 Gravity Wiz | |
*/ | |
class GWCalculatedShipping { | |
private $_orig_field = null; | |
function __construct( $args ) { | |
$this->_args = wp_parse_args( $args, array( | |
'form_id' => false, | |
'field_id' => false | |
) ); | |
add_filter( 'gform_pre_validation', array( $this, 'add_shipping_field' ), 9 ); | |
add_filter( 'gform_pre_render', array( $this, 'restore_original_field' ), 11 ); | |
add_filter( 'gform_admin_pre_render', array( $this, 'add_shipping_field' ), 9 ); | |
} | |
function add_shipping_field( $form ) { | |
if( $this->_args['form_id'] != $form['id'] ) { | |
return $form; | |
} | |
// 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'] ); | |
// 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 ); | |
$field->type = 'shipping'; | |
$field->inputType = 'singleshipping'; | |
$field->inputs = null; | |
$field = GF_Fields::create( $field ); | |
// map calc value as shipping value | |
$_POST[ "input_{$field->id}" ] = rgpost( "input_{$field->id}_2" ); | |
foreach( $form['fields'] as &$_field ) { | |
if( $_field->id == $field->id ) { | |
$_field = $field; | |
} | |
} | |
return $form; | |
} | |
function field( $args = array() ) { | |
return wp_parse_args( $args, array( | |
'id' => false, | |
'formId' => false, | |
'pageNumber' => 1, | |
'adminLabel' => '', | |
'adminOnly' => '', | |
'allowsPrepopulate' => 1, | |
'defaultValue' => '', | |
'description' => '', | |
'content' => '', | |
'cssClass' => '', | |
'errorMessage' => '', | |
'inputName' => '', | |
'isRequired' => '', | |
'label' => 'Shipping', | |
'noDuplicates' => '', | |
'size' => 'medium', | |
'type' => 'shipping', | |
'displayCaption' => '', | |
'displayDescription' => '', | |
'displayTitle' => '', | |
'inputType' => 'singleshipping', | |
'inputs' => '', | |
'basePrice' => '$0.00' | |
) ); | |
} | |
function restore_original_field( $form ) { | |
if( $this->_args['form_id'] != $form['id'] || empty( $this->_orig_field ) ) { | |
return $form; | |
} | |
foreach( $form['fields'] as &$field ) { | |
if( $field['id'] == $this->_orig_field['id'] ) { | |
$field = GF_Fields::create( $this->_orig_field ); | |
break; | |
} | |
} | |
return $form; | |
} | |
} | |
# Configuration | |
new GWCalculatedShipping( array( | |
'form_id' => 1378, | |
'field_id' => 1 | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment