WordPress & web · shop.checkoutCI passing

WooCommerce Pricing Rules

Bulk, tiered, BOGO and cart discounts that match an independent reference to the cent.

View the code Evaluation report Demo tour Run it ›_ last push 5 days ago

0 / 20,000random carts that differ from the independent reference

10 / 10planted bugs caught by the tests (mutation testing)

2.6 msp95 for a 50-line, 50,000-unit cart (reference: about 2 s)

Evaluation report: engine against reference, mutation testing results and the cost of two common shortcuts
Evaluation report: engine against reference, mutation testing results and the cost of two common shortcuts. Screenshot from the project's demo (synthetic data).

demo Demo tour

Screenshots from the running application, with its synthetic demo data. Select one to enlarge.

Bulk and category discounts in a real WooCommerce cart, with the saving per line.
Bulk and category discounts in a real WooCommerce cart, with the saving per line.
The rule list in the admin.
The rule list in the admin.
Editing a tiered rule with schedule and customer roles.
Editing a tiered rule with schedule and customer roles.
Evaluation: reference comparison, mutation testing and the cost of shortcuts.
Evaluation: reference comparison, mutation testing and the cost of shortcuts.

01 The problem

Discount plugins compute prices with floating point and stack percentages by adding them, so carts drift by pennies or go below zero, and big orders slow the checkout down.

02 How it works

  1. Cart lines
  2. Matching rules (schedule, role, category)
  3. Units grouped into buckets
  4. Integer-cent discounts
  5. Price floor
  6. WooCommerce totals

03 What I built

  • A pricing engine in integer cents with rule types for bulk tiers, buy-x-get-y, category and cart discounts, schedules, customer roles and a price floor.
  • Property tests against a separately written unit-by-unit reference, plus mutation testing to prove those tests catch planted bugs.
  • An admin rule editor, cart and product-page badges, and integration tests in real WooCommerce 9.9 and 11.2.

04 What I learned

The first reference shared helpers with the engine, so mutants survived; an independent reference plus boundary cases caught all ten.

Two of my hand-written expected totals were wrong and the engine was right, which is why the comparison is with code, not with my arithmetic.

05 Known limitations

Every repository documents what it doesn't do yet. The main ones:

  • Rules are evaluated per cart; there is no coupon-code integration beyond WooCommerce's own.
  • Tested with WooCommerce 9.9 to 11.2.

06 Stack

PHP 8.1WooCommerceWordPressPHPUnitPHPStanProperty testing

The repository has the design document, operations runbook, full evaluation report and known limitations. All data is synthetic, generated by code in the repository.

run Run it yourself

Every project runs on a laptop: no cloud account and no paid API needed.

git clone https://github.com/codeROCK-IZZY/woocommerce-pricing-rules.git
cd woocommerce-pricing-rules
composer install && vendor/bin/phpunit
php eval/run.php --gate               # writes eval/reports/report.html

↑↓ to move · Enter to open · Esc to close