-
Notifications
You must be signed in to change notification settings - Fork 3
Fix: Upsell Order Bump product visibility issues (Issue #364) #416
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: develop
Are you sure you want to change the base?
Fix: Upsell Order Bump product visibility issues (Issue #364) #416
Conversation
WalkthroughThis PR enhances the Upsell Order Bump module by introducing rigorous validations and improved error handling across its PHP and JavaScript components. It includes checks for product and variation existence, default values for missing inputs, early exits when necessary, and enhanced AJAX interactions. The changes also refine the rendering logic for the bump offer template to ensure the offer product is displayed correctly during checkout. Changes
Sequence Diagram(s)sequenceDiagram
participant UI as User Interface
participant JS as order-bump-custom.js
participant AJAX as Ajax.php
participant Backend as Cart Handler
UI->>JS: Trigger upsell bump action
JS->>AJAX: Send AJAX request with offer details
AJAX->>Backend: Validate product & variation data
Backend-->>AJAX: Return success/error response
AJAX->>JS: Process response, update cart totals
JS->>UI: Refresh UI or revert changes on error
Assessment against linked issues
Poem
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ 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:
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 (
|
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: 0
🧹 Nitpick comments (2)
Includes/Modules/UpsellOrderBump/assets/js/order-bump-custom.js (2)
1-1
: Ensure naming clarity forextraProducts
.
It might be helpful to rename this function to more clearly reflect its purpose, e.g.updateBumpProducts
ortoggleUpsellProduct
. This improves discoverability and maintainability.
31-32
: Consider optional chaining for clarity.
Currently, the code checksif (data && data.success) …
. You could simplify toif (data?.success) …
, though this is a minor readability preference in modern JS.- if (data && data.success) { + if (data?.success) {🧰 Tools
🪛 Biome (1.9.4)
[error] 32-32: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
Includes/Modules/UpsellOrderBump/Includes/Ajax.php
(2 hunks)Includes/Modules/UpsellOrderBump/Includes/OrderBump.php
(2 hunks)Includes/Modules/UpsellOrderBump/assets/js/order-bump-custom.js
(1 hunks)Includes/Modules/UpsellOrderBump/templates/bump-product-front-view.php
(3 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
Includes/Modules/UpsellOrderBump/Includes/OrderBump.php (1)
Includes/Modules/UpsellOrderBump/Includes/Ajax.php (1) (1)
bump_list
(79:101)
🪛 Biome (1.9.4)
Includes/Modules/UpsellOrderBump/assets/js/order-bump-custom.js
[error] 32-32: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (23)
Includes/Modules/UpsellOrderBump/assets/js/order-bump-custom.js (4)
4-8
: Good input validation.
Checking for a validproduct_id
early prevents unnecessary AJAX calls. This improves reliability and user feedback.
16-21
: Solid approach to sanitizing and defaulting values.
By falling back to sensible defaults (0
,false
, etc.), you reduce the chance of runtime errors or malformed requests.
34-40
: Effective error handling and UI rollback.
Re-enabling the checkbox and reverting the check state ensures the UI remains consistent when the operation fails. This enhances user experience and prevents stale UI states.
47-53
: Fail callback is well-handled.
Logging the error and restoring the UI state is crucial for debugging and user feedback.Includes/Modules/UpsellOrderBump/templates/bump-product-front-view.php (6)
8-10
: Initialize variables consistently.
Defining$product_offer_id
and$variation_id
upfront makes the code more readable and prevents uninitialized variable warnings.
12-24
: Robust handling of various product types.
This block correctly differentiates among ‘variation’, ‘simple’, and others. Good practice to cover all product types, reducing future edge-case errors.
27-30
: Early return for invalid products.
Exiting when$product_offer_id
is empty prevents meaningless template output, improving performance and avoiding undefined behavior.
32-46
: Use ofwp_parse_args
for default properties is consistent.
Providing defaults for$bump_info
ensures robust fallback behavior and prevents undefined index issues.
72-72
: Safe fallback image logic.
Ensuring$image_url
falls back to a local asset avoids broken images whenoffer_image_url
is invalid.
89-89
: Display default product title when missing.
This maintains a consistent user interface in the eventoffer_product_title
is not set.Includes/Modules/UpsellOrderBump/Includes/OrderBump.php (6)
54-58
: Avoid further processing when cart is empty.
The early return at line 56 conserves resources and is a clean way to short-circuit if there are no products in the cart.
59-65
: Validate$bump_info
shape before usage.
Skipping invalid bump data reduces risk of runtime errors. Good practice to ensure$bump_info
is an array or object first.
67-73
: Graceful handling of missing offer product.
Theisset
checks and default assignments reduce potential PHP notices, and skipping empty$offer_product_id
is appropriate.
80-84
: Proper product existence check.
Continuing the loop on a missing or invalid product prevents broken logic and enhances overall stability.
113-119
: Simplified approach for matching target products.
Usingarray_intersect
clarifies the condition and avoids confusion about whether all or any matches are needed. This is more readable than counting differences.
122-128
: Consistent logic for matching categories.
Similar to the product matching logic,array_intersect
is a clear and understandable approach for verifying category matches.Includes/Modules/UpsellOrderBump/Includes/Ajax.php (7)
120-121
: Good practice: track product/category IDs in arrays.
Initializing$all_cart_product_ids
and$all_cart_category_ids
increases clarity when re-checking or validating cart contents.
131-134
: Appropriate defaults for missing fields.
Gracefully falling back to0
orfalse
avoids undefined behavior and fosters consistent input handling.
136-142
: Product existence validation is crucial.
Early error response if the product doesn’t exist ensures the checkout remains consistent. This also saves unnecessary computations.
143-156
: Well-structured removal logic forchecked
condition.
Looping through cart items and removing both product and possible variation ensures accurate cart state. The success response is well-defined.
157-186
: Robust addition flow for upsell.
Including a custom price, recalculating totals, and ensuring cart session updates are excellent steps to maintain correct cart data.
188-189
: Graceful exception handling.
Catching exceptions and sending a JSON error fosters clear debugging and a stable user experience.
191-192
: Logical final safeguard.
wp_die()
ensures that no unexpected code executes past the multiplewp_send_json_*
calls.
Fix for Issue #364: Offer Product Doesn't Appear
This PR addresses the issue where offer products don't appear during checkout when using the Upsell Order Bump module.
Root Causes Identified
Logic Error in Display Condition: The original code used an overly restrictive condition to determine if an offer should be displayed, requiring all cart products to match the target products.
Array Initialization Issues: The
$all_cart_category_ids
array wasn't properly initialized before use, causing PHP notices that could prevent checkout bump from displaying.Input Validation Issues: Incomplete validation of incoming product data was causing issues with variable products and special product types.
Error Handling Deficiencies: Inadequate error handling in both PHP and JavaScript made it difficult to diagnose issues.
Changes Made
OrderBump.php:
bump-product-front-view.php:
order-bump-custom.js:
Ajax.php:
Testing
The changes have been tested with various product configurations and checkout scenarios to verify they address the issue without introducing new problems.
Closes #364
Summary by CodeRabbit