-
Couldn't load subscription status.
- Fork 55
Add coupon and refund ratio parameters to order generator #182
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: trunk
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR enhances the order generator with two new parameters for creating more realistic test data: --coupon-ratio and --refund-ratio. These allow users to specify the percentage of orders that should have coupons applied and the percentage of completed orders that should be refunded, respectively.
Key Changes:
- Added
--coupon-ratioparameter to control coupon application percentage with automatic coupon creation when none exist - Added
--refund-ratioparameter to generate full or partial refunds on completed orders - Implemented automatic creation of 6 coupons (3 fixed, 3 percentage) when the store has no existing coupons
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| includes/Generator/Order.php | Implements coupon ratio logic, refund creation functionality, and helper methods for managing coupons and refunds |
| includes/CLI.php | Adds command-line parameter definitions for --coupon-ratio and --refund-ratio |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Add detailed logging when orders cannot be refunded due to empty line items - Consolidate refund creation error logs into single formatted message - Add error logging for invalid order instance check These improvements will help diagnose why some completed orders aren't receiving refunds when --refund-ratio=1.0 is specified. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Adds check to ensure refund amount is greater than 0 before calling wc_create_refund(). This prevents "Invalid refund amount" errors that occur when: - Orders have 100% discount coupons (total = $0) - Line items have $0 totals - Calculation results in 0 or negative amount Logs order ID, calculated amount, and order total when skipping refund. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
The "Invalid refund amount" error occurred because calculated refund amounts slightly exceeded the available order total due to rounding errors in tax calculations. Changes: - Calculate maximum refundable amount (order total - already refunded) - Cap refund amount to maximum available before calling wc_create_refund() - Round both calculated refund and max refund to 2 decimal places - Improve error logging to show order total and already refunded amounts Example of the issue: - Order total: $24851.03 - Calculated refund (with 3 decimal tax): $24851.04 - Result: $0.01 over limit → "Invalid refund amount" error This fix ensures refunds never exceed the mathematically available amount, preventing WooCommerce validation errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
…partial refunds stay under 50% For full refunds, use the order's actual total instead of summing line items to avoid rounding discrepancies that created tiny 0.01 refunds. For partial refunds, ensure the total stays below 50% of the order total by removing items if needed, preventing two partial refunds from fully refunding an order. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
…over-refunding When creating multiple refunds, the code was using original order quantities instead of accounting for items already refunded. This caused second refunds to exceed the original order quantities (e.g., 11 items refunded from an 8-item order). Now tracks refunded quantities per item and only refunds remaining quantities. All refund logic (full items, partial quantities, and fallback) now calculates remaining quantity = original - already_refunded before processing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add division-by-zero guards before all $original_qty divisions - Change parameter checks from !empty() to isset() to support explicit 0 values - Remove unused variable $removed_item in refund amount calculation These changes improve robustness and prevent potential PHP warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- First refunds are created within 2 months of order completion date - Second refunds are created within 1 month of first refund date - Update create_refund() to return refund object instead of boolean - Pass previous refund to second refund for proper date calculation This makes generated refund data more realistic for testing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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.
Nice wprk, @layoutd!
Test refund-ratio=0.5 refunds approximately 50% of orders
wp wc generate orders 20 --status=completed --refund-ratio=0.5
# Check: about 10 orders should have refunds (strikeout prices in orders table)
I noticed that only one refund is listed in the orders table, but several orders have a total amount of 0. Is this expected?
This may be due to a lack of products in the store - afaics, they aren't automatically generated so you have to run |
Summary
This PR adds two new parameters to the order generator for more realistic test data generation, plus a new parameter for the coupon generator:
--coupon-ratio: Apply coupons to a percentage of generated orders--refund-ratio: Refund a percentage of completed orders--discount_type: Specify discount type when generating couponsChanges
Order Generator
--coupon-ratioParameter--couponsflag (same as using--coupons-ratio=1.0)Examples:
--refund-ratioParametercompletedstatusExamples:
Coupon Generator
--discount_typeParameterfixed_cartorpercentfixed_cart)Examples:
Technical Details
Refund Implementation
wc_create_refund()functionrefundedCoupon Implementation
Coupon::generate()method with newdiscount_typeparameterCoupon::get_random()to retrieve existing couponsTesting
Start with 0 orders and 0 coupons!!
Coupon Ratio Tests
Verify 6 coupons are created (3 fixed, 3 percent) when none exist
CONTINUE: test coupon-ratio applies coupons to correct percentage of orders
# Then check: approximately 10 of the above orders should have coupons applied (open all in new tab and look for discounts on first line item)BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersTest coupon-ratio=1.0 applies coupons to all orders
BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersTest backwards compatibility with --coupons flag
wp wc generate orders 10 --coupons # Check: all orders should have coupons (existing behavior)Refund Ratio Tests
BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersTest refund-ratio=0.5 refunds approximately 50% of orders
wp wc generate orders 20 --status=completed --refund-ratio=0.5 # Check: about 10 orders should have refunds (strikeout prices in orders table)BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersTest refund-ratio=1.0 refunds all completed orders
wp wc generate orders 20 --status=completed --refund-ratio=1.0 --date-start=2023-01-01 --date-end=2023-01-31 # Check: all 20 orders should have refunds (strikeout prices in orders table)CONTINUE: Verify refund amounts match line item totals (not 0.00)
# Check in WP Admin: refund totals should match refunded items totals (look at some individual orders confirm line item red numbers add up to refund line item)CONTINUE Verify refund dates are realistic
# Check: all refunds should basically be in Q1 2023 (~2 months for first, ~1 additional month if there's a second refund) Confirm line item refund date is in Q1 2023 and not todayCONTINUE Verify partial refund reasons show correct product/item counts
# Check: partial refunds should have reasons like "Partial refund - 2 products, 3 items" (look at individual order line items and refund item)BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersVerify ~25% of partial refunds receive a second refund
BULK DELETE all orders with WP Admin interface
/wp-admin/admin.php?page=wc-ordersTest refund-ratio only affects completed orders
wp wc generate orders 10 --status=processing --refund-ratio=1.0 # Check: no orders should have refunds (only completed status can be refunded)Discount Type Tests
BULK DELETE all coupons with WP Admin interface
/wp-admin/edit.php?post_type=shop_couponTest creating percentage coupons with discount_type parameter
wp wc generate coupons 5 --discount_type=percent --min=5 --max=25 # Check: all 5 coupons should be percentage type with 5-25% discountBULK DELETE all coupons with WP Admin interface
/wp-admin/edit.php?post_type=shop_couponTest creating fixed_cart coupons with discount_type parameter
wp wc generate coupons 5 --discount_type=fixed_cart --min=10 --max=50 # Check: all 5 coupons should be fixed_cart type with $10-$50 discountBULK DELETE all coupons with WP Admin interface
/wp-admin/edit.php?post_type=shop_couponVerify default behavior without discount_type (should be fixed_cart)
wp wc generate coupons 5 # Check: coupons should default to fixed_cart typeIntegration Tests
/wp-admin/admin.php?page=wc-orderswp wc generate orders 20 --status=completed --coupon-ratio=0.5 --refund-ratio=0.3 # Check: ~10 orders with coupons, ~6 orders with refunds