Skip to content

Fix wc-list-orders fatal TypeError on refunds in range (#21) - #22

Merged
trueqap merged 1 commit into
mainfrom
fix/wc-list-orders-refund-typerror-21
Aug 6, 2026
Merged

Fix wc-list-orders fatal TypeError on refunds in range (#21)#22
trueqap merged 1 commit into
mainfrom
fix/wc-list-orders-refund-typerror-21

Conversation

@trueqap

@trueqap trueqap commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #21.

Problem

wc-list-orders threw a fatal TypeError (HTTP 500, surfaced over MCP as a generic error) whenever a refund fell in the queried date range, making the ability unusable on any store that issues refunds:

TypeError: OrderManager::format_order(): Argument #1 ($order) must be of type
WC_Order, Automattic\WooCommerce\Admin\Overrides\OrderRefund given,
called in .../OrderManager.php on line 58

list_orders() built the wc_get_orders() query without a type, so under HPOS the result also contained WC_Order_Refund objects. format_order() is typed \WC_Order and rejected the first refund.

Fix

Restrict the query to 'type' => 'shop_order'. This excludes refunds and keeps total / max_num_pages honest — a per-row instanceof guard would stop the fatal but leave the counts (and pagination) wrong.

This is the only wc_get_orders() call site; the report/aggregator code already scopes to shop_order via explicit SQL.

Tests

  • Added tests/Unit/Services/WooCommerce/OrderManagerTest.php — RED before the fix, GREEN after — asserting the query is restricted to shop_order (plus a reusable WooCommerce class stub and an arg-capturing wc_get_orders stub in the shared stub file).
  • Full suite green (381 tests), composer phpcs clean, PHPStan L5 no errors.

Verified end-to-end on real WordPress

On the WC 10.8.1 + HPOS test site: created a scratch order + refund, reproduced the exact TypeError on the deployed 1.3.1, deployed the fix, confirmed list_orders returns orders with the refund correctly excluded and no fatal, then removed the scratch data.

Bumps to 1.3.2 (plugin header + constant, readme.txt stable tag + changelog, CHANGELOG.md).

list_orders() built the wc_get_orders() query without a `type`, so under
HPOS the result also contained WC_Order_Refund objects. format_order() is
typed \WC_Order and threw a fatal TypeError on the first refund, making
wc-list-orders (HTTP 500 / generic MCP error) unusable on any store that
had a refund in the queried range.

Restrict the query to 'type' => 'shop_order', which excludes refunds and
keeps total / max_num_pages honest (a per-row instanceof guard would not).

Regression test:
OrderManagerTest::test_list_orders_restricts_query_to_shop_order_type
(adds a WooCommerce class stub + an arg-capturing wc_get_orders stub).

Reproduced and verified end-to-end on WooCommerce 10.8.1 with HPOS.
Bump to 1.3.2.
@trueqap
trueqap merged commit f58c431 into main Aug 6, 2026
7 checks passed
@trueqap
trueqap deleted the fix/wc-list-orders-refund-typerror-21 branch August 6, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wc-list-orders: fatal TypeError when a refund falls in the queried range

1 participant