-
Notifications
You must be signed in to change notification settings - Fork 206
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
fix: Improve vendor coupon validation for various discount item types and empty cart scenarios #2351
fix: Improve vendor coupon validation for various discount item types and empty cart scenarios #2351
Conversation
WalkthroughThe changes enhance the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Cart
participant CouponSystem
User->>Cart: Check cart status
Cart->>CouponSystem: Verify cart is not empty
alt Cart is not empty
CouponSystem->>Cart: Retrieve discount items
loop Process items
Cart->>CouponSystem: Check item validity
alt Item is valid
CouponSystem->>CouponSystem: Process valid item
else Item is invalid
CouponSystem->>Cart: Skip invalid item
end
end
else Cart is empty
CouponSystem->>User: No items to process
end
Poem
Tip Early access features: enabledWe are currently testing the following features in early access:
Note:
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 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
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- includes/Order/Hooks.php (1 hunks)
Additional comments not posted (5)
includes/Order/Hooks.php (5)
325-325
: Ensure cart is not empty before processing items.The condition
if ( WC()->cart && ! WC()->cart->is_empty() )
ensures that the cart is not empty before processing items. This improves robustness by preventing unnecessary processing when there are no items in the cart.The code changes are approved.
333-334
: Check if the item object is set before processing.The condition
if ( ! isset( $item->object ) )
ensures that theobject
property exists on each item before proceeding. This prevents potential errors from undefined item references.The code changes are approved.
337-339
: Retrieve product ID for WC_Order_Item_Product objects.The condition
if ( is_a( $item->object, 'WC_Order_Item_Product' ) )
retrieves the product ID directly from the object when it is an instance ofWC_Order_Item_Product
. This ensures that valid product IDs are collected.The code changes are approved.
341-343
: Check for product_id or variation_id in array-based items.The condition
if ( is_array( $item->object ) && ( isset( $item['product_id'] ) || isset( $item['variation_id'] ) ) )
checks for eitherproduct_id
orvariation_id
in array-based items and assigns the appropriate value toitem_id
. This ensures that valid product IDs are collected.The code changes are approved.
345-346
: Skip processing if item_id is not set.The condition
if ( ! $item_id )
ensures that the code skips processing ifitem_id
is not set. This prevents potential errors from undefined item references.The code changes are approved.
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
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- includes/Order/Hooks.php (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- includes/Order/Hooks.php
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
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- includes/Order/Hooks.php (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- includes/Order/Hooks.php
Issue: Admin is unable to add coupons to new orders Please refer to the video attached for reference @mralaminahamed bhai |
All Submissions:
Changes proposed in this Pull Request:
This PR fixes an issue with vendor coupon validation when processing discount items. The
ensure_vendor_coupon
method has been updated to properly handle different scenarios when validating coupons, especially when the cart is empty or when dealing with order items.Key changes in item validation from discounts:
When cart items are not found:
WC()->cart
exists and is not empty before processing cart items.Handling different discount item data types:
The updated code now handles three scenarios for discount items:
a. WC_Order_Item_Product objects: Uses
get_product_id()
method to get the product ID.b. Array-based items: Checks for 'product_id' or 'variation_id' keys to determine the item ID.
c. Other types: Skips items that don't match the above types.
This approach ensures that the method can handle various item representations that may occur in different contexts (e.g., cart, order, or custom discount scenarios).
Example Datasets:
To better understand the changes, here are two example datasets that the code now handles correctly:
Dataset 1 (Cart Item):
Dataset 2 (Order Item):
The updated code can now correctly process both these datasets, ensuring proper vendor coupon validation regardless of whether the items come from the cart or an order.
Related Pull Request(s)
Closes
How to test the changes in this Pull Request:
Changelog entry
Fix: Vendor coupon validation from discount items
Improved the vendor coupon validation process to correctly handle various scenarios, including empty carts and order items. This ensures that vendor coupons are properly validated and applied in all situations.
Before Changes
Previously, the coupon validation process did not properly handle cases where the cart was empty or when dealing with order items. This could lead to incorrect validation results and potential issues with coupon application.
Specifically, the code did not differentiate between different types of discount items, which could cause errors or incorrect validations when processing orders or applying coupons in certain scenarios.
After Changes
The updated code now properly checks for cart items and handles different types of discount items, including
WC_Order_Item_Product
and array-based items. This ensures that vendor coupons are correctly validated in all scenarios.Key improvements:
Feature Video (optional)
N/A
PR Self Review Checklist:
FOR PR REVIEWER ONLY:
Summary by CodeRabbit
New Features
Bug Fixes