Skip to content

Commit 73e6bef

Browse files
committed
gw-product-quantity-conditional.php: Added snippet to use quantity value of Product Field as a conditional lopic field.
1 parent ad33967 commit 73e6bef

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Forms // Product Quantity Conditional
4+
*
5+
* Instruction Video: https://www.loom.com/share/38547a28853f4706b10cfdb42eaa45ca
6+
*
7+
* Adds quantity field options to conditional logic for Single Product fields.
8+
*
9+
* Plugin Name: GF Product Quantity Conditional
10+
* Plugin URI: https://gravitywiz.com/
11+
* Description: Adds quantity field options to conditional logic for Single Product fields that don't have dedicated quantity fields
12+
* Author: Gravity Wiz
13+
* Version: 0.1
14+
* Author URI: https://gravitywiz.com
15+
*/
16+
class GW_Product_Quantity_Conditional {
17+
18+
private static $_instance = null;
19+
20+
public static function get_instance() {
21+
if ( self::$_instance == null ) {
22+
self::$_instance = new self;
23+
}
24+
return self::$_instance;
25+
}
26+
27+
public function __construct() {
28+
29+
add_action( 'admin_init', array( $this, 'init' ) );
30+
31+
}
32+
33+
public function init() {
34+
35+
add_action( 'admin_print_scripts', array( $this, 'enqueue_admin_scripts' ) );
36+
37+
}
38+
39+
public function enqueue_admin_scripts() {
40+
41+
if ( ! $this->is_gravity_forms_page() ) {
42+
return;
43+
}
44+
45+
?>
46+
<script type="text/javascript">
47+
window.gwProductQuantityConditional = {
48+
49+
// Check if product has dedicated quantity field
50+
productHasQuantityField: function(fieldId, form) {
51+
for(var i = 0; i < form.fields.length; i++) {
52+
var field = form.fields[i];
53+
if(field.type == 'quantity' && field.productId == fieldId) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
},
59+
60+
// Check if field is a custom quantity field
61+
isCustomQtyField: function(fieldId) {
62+
return typeof fieldId === 'string' && fieldId.indexOf('quantity_') === 0;
63+
},
64+
65+
// Get the product field ID from a quantity field ID
66+
getCustomQtyFieldId: function(fieldId) {
67+
return fieldId.replace('quantity_', '');
68+
},
69+
70+
isQtyField: function(fieldId) {
71+
var field = GetFieldById(fieldId);
72+
return field && field.type == 'quantity';
73+
}
74+
75+
};
76+
77+
gform.addFilter('gform_conditional_logic_fields', function(options, form, selectedFieldId) {
78+
for(var i = 0; i < form.fields.length; i++) {
79+
var field = form.fields[i];
80+
81+
if(field.type != 'product' ||
82+
field.inputType != 'singleproduct' ||
83+
gwProductQuantityConditional.productHasQuantityField(field.id, form) ||
84+
field.disableQuantity) {
85+
continue;
86+
}
87+
88+
options.push({
89+
label: (field.adminLabel ? field.adminLabel : field.label) + ' (Quantity)',
90+
value: field.id,
91+
});
92+
}
93+
return options;
94+
});
95+
</script>
96+
<?php
97+
}
98+
99+
public function is_gravity_forms_page() {
100+
return method_exists( 'GFForms', 'is_gravity_page' ) && GFForms::is_gravity_page();
101+
}
102+
103+
}
104+
105+
new GW_Product_Quantity_Conditional();

0 commit comments

Comments
 (0)