Include WC Product Quantity in Total Field
Include the WooCommerce product quantity in the Gravity Forms Total field calculation.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gspc-include-wc-quantity-in-total-field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Gravity Shop // Product Configurator // Include WC Product Quantity in Total Field
* https://gravitywiz.com/documentation/gravity-shop-product-configurator/
*
* Include the WooCommerce product quantity in the Gravity Forms Total field calculation.
*
* Plugin Name: GSPC Include WC Product Quantity in Total
* Plugin URI: https://gravitywiz.com/documentation/gravity-shop-product-configurator/
* Description: Include the WooCommerce product quantity in the Gravity Forms Total field calculation.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: https://gravitywiz.com
*/
class GSPC_Include_WC_Product_Quantity_In_Total {
public $_args;
public function __construct( $args = array() ) {
// Set default arguments, parse against the provided arguments, and store for use throughout the class.
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ) );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );
add_filter( 'gform_pre_process', array( $this, 'bypass_total_validation' ) );
}
public function load_form_script( $form ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.<?php echo __CLASS__; ?> = function( args ) {
var self = this;
self.formId = args.formId;
self.init = function() {
let $wc_quantity = $( 'form#gform_' + self.formId ).find( 'input[name="quantity"]' );
$wc_quantity.on( 'change', function() {
gformCalculateTotalPrice( self.formId );
} );
gform.addFilter( 'gform_product_total', function( total, formId ) {
if ( formId == self.formId ) {
total *= gformToNumber( $wc_quantity.val() );
}
return total;
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
);
$script = 'new ' . __CLASS__ . '( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
function bypass_total_validation( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
foreach ( $form['fields'] as &$field ) {
if ( $field->type === 'total' ) {
$field->validateTotal = false;
}
}
return $form;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
}
}
// Configuration
new GSPC_Include_WC_Product_Quantity_In_Total( array(
// Update "123" to your form ID
'form_id' => 123,
) );