Skip to content

gpi-waiting-list.php: Added waiting list message on order summary page. #1117

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

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2950621761/84154

Summary

When using the waiting list snippet feature on a Product choice field, it doesn't show any sign or indication in the order summary on the entry page, that an exhausted selected choice is on the waiting list.

BEFORE:
Screenshot 2025-06-05 at 1 11 42 AM

AFTER:
Screenshot 2025-06-05 at 1 11 21 AM

Copy link

coderabbitai bot commented Jun 4, 2025

Walkthrough

A new filter hook is introduced to modify the order summary markup in the waiting list functionality. The code now appends a waiting list message to product names in the order summary when their available stock is zero or less. This is achieved by parsing and updating the HTML markup before it is displayed.

Changes

File(s) Change Summary
gp-inventory/gpi-waiting-list.php Added a filter hook to modify order summary markup; introduced add_waiting_list_to_order_summary method to append waiting list messages for out-of-stock products.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant OrderSummary
    participant GPI_Waiting_List

    User->>OrderSummary: View order summary
    OrderSummary->>GPI_Waiting_List: Apply gform_order_summary filter (markup, form, entry)
    GPI_Waiting_List->>GPI_Waiting_List: add_waiting_list_to_order_summary()
    GPI_Waiting_List->>OrderSummary: Return modified markup with waiting list messages
    OrderSummary->>User: Display updated order summary
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jun 4, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 23a31e9

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
gp-inventory/gpi-waiting-list.php (2)

291-303: Optimize nested loops and improve string comparison reliability.

The nested loops could be inefficient with many products. Also, direct string comparison may fail due to whitespace or HTML entity differences.

-foreach ( $productNodes as $node ) {
-    foreach ( $products['products'] as $product_id => $product ) {
-        if ( $product['name'] == $node->nodeValue ) {
+// Create a map for efficient lookup
+$product_map = array();
+foreach ( $products['products'] as $product_id => $product ) {
+    $product_map[ trim( $product['name'] ) ] = array( 'id' => $product_id, 'product' => $product );
+}
+
+foreach ( $productNodes as $node ) {
+    $node_text = trim( $node->nodeValue );
+    if ( isset( $product_map[ $node_text ] ) ) {
+        $product_id = $product_map[ $node_text ]['id'];
+        $product = $product_map[ $node_text ]['product'];

294-296: Extract inventory instance determination to reduce code duplication.

This logic is duplicated from the add_waiting_list_to_single_product method. Consider extracting it to a private helper method.

+private function get_inventory_instance( $field ) {
+    return gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
+}
+
 // Then in both methods:
-$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
+$gpi_instance = $this->get_inventory_instance( $field );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc5b10d and 23a31e9.

📒 Files selected for processing (1)
  • gp-inventory/gpi-waiting-list.php (2 hunks)
🔇 Additional comments (1)
gp-inventory/gpi-waiting-list.php (1)

70-70: LGTM: Filter hook registration is correct.

The hook registration follows WordPress best practices with appropriate priority and argument count.

Comment on lines +305 to +306
$markup = $dom->saveHTML();
return $markup;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Preserve original markup structure when returning modified HTML.

The saveHTML() method may add DOCTYPE and wrapper elements that weren't in the original markup, potentially breaking the layout.

-$markup = $dom->saveHTML();
-return $markup;
+// Get only the body content to preserve original structure
+$body = $dom->getElementsByTagName( 'body' )->item( 0 );
+if ( $body ) {
+    $markup = '';
+    foreach ( $body->childNodes as $child ) {
+        $markup .= $dom->saveHTML( $child );
+    }
+} else {
+    $markup = $dom->saveHTML();
+}
+return $markup;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$markup = $dom->saveHTML();
return $markup;
// Get only the body content to preserve original structure
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
if ( $body ) {
$markup = '';
foreach ( $body->childNodes as $child ) {
$markup .= $dom->saveHTML( $child );
}
} else {
$markup = $dom->saveHTML();
}
return $markup;
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 305 to 306, the use of
$dom->saveHTML() returns the entire document including added DOCTYPE and wrapper
elements, which can alter the original markup structure and break layout. To fix
this, extract and return only the inner HTML of the specific element you
modified instead of the whole document. This preserves the original markup
structure without extra wrappers.

Comment on lines +285 to +287
libxml_use_internal_errors( true );
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Handle DOM parsing errors and consider markup preservation.

The DOMDocument parsing could fail or alter the original markup structure. Consider adding error handling and preserving the original markup format.

 libxml_use_internal_errors( true );
 $dom = new DOMDocument();
-$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
+$success = $dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
+if ( ! $success ) {
+    return $markup; // Return original markup if parsing fails
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
libxml_use_internal_errors( true );
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
libxml_use_internal_errors( true );
$dom = new DOMDocument();
$success = $dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
if ( ! $success ) {
return $markup; // Return original markup if parsing fails
}
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 285 to 287, the DOMDocument
loadHTML call lacks error handling and may alter the original markup. Add checks
to capture and handle any parsing errors from libxml after calling loadHTML, and
consider preserving the original markup by storing it before parsing or using
alternative methods that maintain the markup structure.

Comment on lines +294 to +299
$field = GFAPI::get_field( $form, $product_id );
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
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 validation for inventory field compatibility.

The code assumes the field is an inventory field but doesn't validate this before calling inventory methods, which could cause errors.

 $field        = GFAPI::get_field( $form, $product_id );
+
+// Validate that this is an inventory field
+if ( ! gp_inventory_type_simple()->is_applicable_field( $field ) && 
+     ! gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
+    continue;
+}
+
 $gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$field = GFAPI::get_field( $form, $product_id );
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
$field = GFAPI::get_field( $form, $product_id );
// Validate that this is an inventory field
if ( ! gp_inventory_type_simple()->is_applicable_field( $field ) &&
! gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
continue;
}
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' )
? gp_inventory_type_simple()
: gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 294 to 299, the code assumes
the field is an inventory field without validating it, which may cause errors
when calling inventory methods. Add a check to confirm the field is indeed an
inventory field before proceeding with inventory-related operations. If the
field is not compatible, skip the inventory logic to prevent potential errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant