Skip to content

gw-product-quantity-conditional.php: Added snippet to use quantity value of Product Field as a conditional logic field. #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions gravity-forms/gw-product-quantity-conditional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Gravity Wiz // Gravity Forms // Product Quantity Conditional
*
* Instruction Video: https://www.loom.com/share/38547a28853f4706b10cfdb42eaa45ca
*
* Adds quantity field options to conditional logic for Single Product fields.
*
* Plugin Name: GF Product Quantity Conditional
* Plugin URI: https://gravitywiz.com/
* Description: Adds quantity field options to conditional logic for Single Product fields that don't have dedicated quantity fields
* Author: Gravity Wiz
* Version: 0.1
* Author URI: https://gravitywiz.com
*/
class GW_Product_Quantity_Conditional {

private static $_instance = null;

public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new self;
}
return self::$_instance;
}

public function __construct() {

add_action( 'admin_init', array( $this, 'init' ) );

}
Comment on lines +27 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add Gravity Forms dependency check.

The constructor doesn't verify that Gravity Forms is active, which could lead to fatal errors if the plugin is activated without Gravity Forms.

 	public function __construct() {
-
+		// Check if Gravity Forms is active
+		if ( ! class_exists( 'GFForms' ) ) {
+			add_action( 'admin_notices', array( $this, 'gravity_forms_required_notice' ) );
+			return;
+		}
+
 		add_action( 'admin_init', array( $this, 'init' ) );
-
 	}

Add the notice method:

public function gravity_forms_required_notice() {
	echo '<div class="notice notice-error"><p>GF Product Quantity Conditional requires Gravity Forms to be installed and activated.</p></div>';
}
🤖 Prompt for AI Agents
In gravity-forms/gw-product-quantity-conditional.php around lines 27 to 31, the
constructor lacks a check to verify if Gravity Forms is active, risking fatal
errors if this plugin is activated without Gravity Forms. Add a check in the
constructor to see if the Gravity Forms class or function exists; if not, hook a
method that displays an admin notice about the missing dependency. Implement the
gravity_forms_required_notice method to output an admin error notice informing
users that Gravity Forms must be installed and activated.


public function init() {

add_action( 'admin_print_scripts', array( $this, 'enqueue_admin_scripts' ) );

}

public function enqueue_admin_scripts() {

if ( ! $this->is_gravity_forms_page() ) {
return;
}

?>
<script type="text/javascript">
window.gwProductQuantityConditional = {

// Check if product has dedicated quantity field
productHasQuantityField: function(fieldId, form) {
for(var i = 0; i < form.fields.length; i++) {
var field = form.fields[i];
if(field.type == 'quantity' && field.productId == fieldId) {
return true;
}
}
return false;
},

// Check if field is a custom quantity field
isCustomQtyField: function(fieldId) {
return typeof fieldId === 'string' && fieldId.indexOf('quantity_') === 0;
},

// Get the product field ID from a quantity field ID
getCustomQtyFieldId: function(fieldId) {
return fieldId.replace('quantity_', '');
},

isQtyField: function(fieldId) {
var field = GetFieldById(fieldId);
return field && field.type == 'quantity';
}

};

gform.addFilter('gform_conditional_logic_fields', function(options, form, selectedFieldId) {
for(var i = 0; i < form.fields.length; i++) {
var field = form.fields[i];

if(field.type != 'product' ||
field.inputType != 'singleproduct' ||
gwProductQuantityConditional.productHasQuantityField(field.id, form) ||
field.disableQuantity) {
continue;
}

options.push({
label: (field.adminLabel ? field.adminLabel : field.label) + ' (Quantity)',
value: field.id,
});
}
return options;
});
</script>
<?php
}

public function is_gravity_forms_page() {
return method_exists( 'GFForms', 'is_gravity_page' ) && GFForms::is_gravity_page();
}

}

new GW_Product_Quantity_Conditional();
Loading