-
Notifications
You must be signed in to change notification settings - Fork 90
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
base: master
Are you sure you want to change the base?
Conversation
…value of Product Field as a conditional lopic field.
WalkthroughA new plugin file introduces the Changes
Sequence Diagram(s)sequenceDiagram
participant AdminUser
participant WP_Admin
participant GW_Product_Quantity_Conditional
participant GravityForms_AdminPage
participant JS_Script
AdminUser->>WP_Admin: Accesses Gravity Forms admin page
WP_Admin->>GW_Product_Quantity_Conditional: Triggers admin_init
GW_Product_Quantity_Conditional->>GW_Product_Quantity_Conditional: init()
GW_Product_Quantity_Conditional->>GravityForms_AdminPage: Checks if GF page
GW_Product_Quantity_Conditional->>GravityForms_AdminPage: Enqueues JS script
AdminUser->>GravityForms_AdminPage: Edits form conditional logic
GravityForms_AdminPage->>JS_Script: Loads JS
JS_Script->>GravityForms_AdminPage: Augments conditional logic fields with quantity options
Suggested reviewers
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
gw-product-quantity-conditional.php
: Added snippet to use quantity value of Product Field as a conditional lopic field.gw-product-quantity-conditional.php
: Added snippet to use quantity value of Product Field as a conditional logic field.
|
1 similar comment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (5)
gravity-forms/gw-product-quantity-conditional.php (5)
20-24
: Use strict comparison in singleton pattern.The singleton implementation uses loose comparison which is less reliable than strict comparison.
- if ( self::$_instance == null ) { + if ( self::$_instance === null ) {
46-75
: Optimize JavaScript object structure and add error handling.The JavaScript object has some inefficiencies and missing error handling that could be improved.
<script type="text/javascript"> window.gwProductQuantityConditional = { // Check if product has dedicated quantity field productHasQuantityField: function(fieldId, form) { + if (!form || !form.fields) return false; 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) { + if (typeof GetFieldById !== 'function') return false; var field = GetFieldById(fieldId); return field && field.type == 'quantity'; } };
77-94
: Enhance conditional logic filter with validation and performance optimization.The filter logic should include better validation and could be optimized for performance.
gform.addFilter('gform_conditional_logic_fields', function(options, form, selectedFieldId) { + if (!form || !form.fields || !Array.isArray(form.fields)) { + return options; + } + for(var i = 0; i < form.fields.length; i++) { var field = form.fields[i]; + // Skip if field is not valid + if (!field || !field.type || !field.id) { + continue; + } + 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; });
88-92
: Add fallback for missing field labels.The label concatenation should handle cases where both adminLabel and label might be missing.
options.push({ - label: (field.adminLabel ? field.adminLabel : field.label) + ' (Quantity)', + label: (field.adminLabel || field.label || 'Product Field ' + field.id) + ' (Quantity)', value: field.id, });
105-105
: Consider using get_instance() method for consistency.For consistency with the singleton pattern, consider using the get_instance() method instead of direct instantiation.
-new GW_Product_Quantity_Conditional(); +GW_Product_Quantity_Conditional::get_instance();
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gravity-forms/gw-product-quantity-conditional.php
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Danger JS
- GitHub Check: Danger JS
🔇 Additional comments (1)
gravity-forms/gw-product-quantity-conditional.php (1)
1-15
: Plugin header looks good with clear documentation.The plugin metadata is well-structured with appropriate description and links to instruction materials.
public function __construct() { | ||
|
||
add_action( 'admin_init', array( $this, 'init' ) ); | ||
|
||
} |
There was a problem hiding this comment.
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.
@saifsultanc Does this work on submission as well? I was expecting to see some PHP validation but maybe it's not required? |
@spivurno Yup, it's works on submission. Tested it out. No PHP validation required! |
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2951687684/84205
Summary
Targeting Single Product field's quantity input to be applicable in Gravity Forms' regular conditional logic.
Snippet Demo:
https://www.loom.com/share/38547a28853f4706b10cfdb42eaa45ca