|
| 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 | + |
| 61 | + gform.addFilter('gform_conditional_logic_fields', function(options, form, selectedFieldId) { |
| 62 | + for(var i = 0; i < form.fields.length; i++) { |
| 63 | + var field = form.fields[i]; |
| 64 | + |
| 65 | + if(field.type != 'product' || |
| 66 | + field.inputType != 'singleproduct' || |
| 67 | + gwProductQuantityConditional.productHasQuantityField(field.id, form) || |
| 68 | + field.disableQuantity) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + options.push({ |
| 73 | + label: (field.adminLabel ? field.adminLabel : field.label) + ' (Quantity)', |
| 74 | + value: field.id, |
| 75 | + }); |
| 76 | + } |
| 77 | + return options; |
| 78 | + }); |
| 79 | + </script> |
| 80 | + <?php |
| 81 | + } |
| 82 | + |
| 83 | + public function is_gravity_forms_page() { |
| 84 | + return method_exists( 'GFForms', 'is_gravity_page' ) && GFForms::is_gravity_page(); |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +new GW_Product_Quantity_Conditional(); |
0 commit comments