Skip to content

Fix checkout fatal when a cart line price is not numeric - #2997

Merged
ismaeldcom merged 4 commits into
trunkfrom
wootax-339-checkout-fatal-when-a-cart-line-has-no-price
Aug 26, 2026
Merged

Fix checkout fatal when a cart line price is not numeric#2997
ismaeldcom merged 4 commits into
trunkfrom
wootax-339-checkout-fatal-when-a-cart-line-has-no-price

Conversation

@ismaeldcom

Copy link
Copy Markdown
Contributor

Description

  • Guard the cart line item build in get_line_items() with is_numeric( $unit_price ), so a malformed price never reaches the totals arithmetic.

wc_format_decimal( $product->get_price() ) reduces a malformed price to '' or '-', and calculate_totals() multiplies that by the quantity, which is a TypeError on PHP 8 that takes the whole checkout page down. The way in is a pricing plugin filtering woocommerce_product_get_price after the item is already in the cart, since WooCommerce will not let you add a product with no price.

is_numeric() rather than a truthiness check because '-' is truthy and would still fatal, and '0' is falsy and must not be dropped, see test_zero_amount_response_persists_real_itemized_rates() for what the response to a $0 line carries.

The skipped line gets no tax row. Nothing is lost by that, on trunk it got none either because the page died and took the rest of the cart's tax with it.

Related issue(s)

Closes WOOTAX-339

Steps to reproduce & screenshots/GIFs

  • Check out trunk and enable automated taxes.
  • Set the store address to a US one, for example 1 Main St, Denver, CO 80202.
  • Log in as a customer whose billing and shipping address is the same state, with a postcode and city filled in. Without them the tax call aborts and nothing happens.
  • Add this to a mu-plugin. Filtering globally instead would make the product unpurchasable and WooCommerce would drop it from the cart.
add_action( 'woocommerce_after_calculate_totals', function () {
	add_filter( 'woocommerce_product_get_price', function () { return '-'; }, 99, 0 );
}, 19 );
add_action( 'woocommerce_after_calculate_totals', function () {
	remove_all_filters( 'woocommerce_product_get_price', 99 );
}, 21 );
  • Add a product to the cart and open the checkout page.
  • The page should show Uncaught TypeError: Unsupported operand types: string * int in class-wc-connect-taxjar-integration.php:614.
  • Check out this PR branch and reload the checkout.
  • The page should render, with its tax rows in the order table.

A zero price must still be sent

  • Enable debug_logging_enabled so the WCS Tax log records Requesting: taxjar/v2/taxes.
  • Remove the mu-plugin, then put a normally priced product and a product priced 0 in the cart.
  • Open the checkout on trunk and on this branch.
  • The logged request should carry "unit_price":"0" on both, and the order totals should be identical.

Checklist

  • unit tests
  • changelog.txt entry added
  • readme.txt entry added

@ismaeldcom ismaeldcom self-assigned this Aug 26, 2026
@ismaeldcom
ismaeldcom marked this pull request as ready for review August 26, 2026 10:36
@ismaeldcom
ismaeldcom requested a lite review from Copilot August 26, 2026 10:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents a PHP 8 checkout fatal in the TaxJar integration when a pricing extension (or other customization) makes a cart line’s unit price non-numeric after the item is already in the cart, by guarding get_line_items().

Changes:

  • Skip building TaxJar line items when unit_price is non-numeric (while still allowing 0).
  • Add unit tests covering non-numeric prices being skipped and zero prices being preserved.
  • Add changelog/readme entries for the fix.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
classes/class-wc-connect-taxjar-integration.php Adds an is_numeric( $unit_price ) guard when building checkout line items to avoid PHP 8 arithmetic TypeErrors.
tests/php/test-class-wc-connect-taxjar-integration.php Adds tests for skipping non-numeric prices and preserving $0 line items; extends teardown cleanup for the price filter.
changelog.txt Documents the checkout fatal fix in the plugin changelog.
readme.txt Documents the checkout fatal fix in the readme changelog section.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread classes/class-wc-connect-taxjar-integration.php
Comment thread tests/php/test-class-wc-connect-taxjar-integration.php
@ismaeldcom
ismaeldcom merged commit 2c822b4 into trunk Aug 26, 2026
9 checks passed
@ismaeldcom
ismaeldcom deleted the wootax-339-checkout-fatal-when-a-cart-line-has-no-price branch August 26, 2026 13:02
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.

2 participants