#69182·woocommerce

Shipping tests: flat rate cost

Author: linear[bot]Created Sep 29, 2026Updated Sep 29, 2026
LabelsShippingplugin: woocommercefocus: unit testsRubikblock: cartblock: checkout

What this covers

A merchant enters a flat rate cost, either a fixed amount or a formula, and the shopper should see exactly that amount on the rate in the Cart and Checkout blocks. This issue pins that relationship: every way the cost can be written, and what the blocks must show for it.

Server-only. Flat rate arithmetic happens entirely in WC_Shipping_Flat_Rate and the cart totals; the blocks render what the Store API hands them, so there is no client behaviour specific to this setting.

Expected behaviour

  • A fixed amount appears unchanged on the rate
  • A cost of zero produces a free rate that is still listed
  • Formulas over quantity, cart total and weight resolve correctly
  • Minimum and maximum bounds on a percentage fee are respected
  • Per shipping class costs apply, including the difference between charging each class and charging only the most expensive
  • A separate cost applies to items with no shipping class
  • A coupon lowers the cart total, so a formula based on that total falls with it

Where it breaks

  • A blank cost removes the method entirely rather than making it free, which a merchant can easily misread
  • An invalid expression is rejected at save time
  • A formula dividing by weight is rejected at save time, since weight can be zero
  • A bound written as zero is silently ignored
  • A store using a comma decimal separator still parses the formula
  • A shipping class is deleted while its cost row survives in the database

Already covered

tests/php/includes/shipping/flat-rate/class-wc-shipping-flat-rate-test.php holds 16 tests over evaluate_cost() and sanitize_cost(), reached as protected methods through closures bound to the method instance, with qty and cost passed in directly: decimal and thousand separators in several locales, percent fees including a comma locale, acceptance and rejection of math expressions, weight substitution, a missing weight argument and a non-numeric weight arriving through the filter. None of them produce a rate or touch a shipping class.

WeightPlaceholderTest adds 9 tests over package weight: summing only shippable items, using the parent weight for variations, zero for empty or missing contents, ignoring malformed items, clamping a negative product weight and never subtracting one, and expand() itself. Weight handling is therefore guarded.

calculate_shipping() demonstrably runs, and a formula is already pinned end to end. class-wc-cart-shipping-rounding-test.php::test_percentage_shipping_with_tax_rounding_issue_62692 builds a real zone, puts '[fee percent="15"]' on the flat rate instance, calls WC()->cart->calculate_totals() and asserts get_shipping_total() is '16.58', fifteen percent of a 110.50 subtotal with the rounding that issue was about. The test's own comment spells out the path it exercises: calculate_totals() to WC_Shipping_Zone to WC_Shipping_Flat_Rate::evaluate_cost() to fee(). That also settles that the fee base is the package cost for a single-item cart.

class-wc-shipping-test.php constructs real WC_Shipping_Flat_Rate( 1 ), WC_Shipping_Free_Shipping( 1 ) and WC_Shipping_Local_Pickup( 1 ) instances and puts them through WC_Shipping::calculate_shipping_for_package(), but asserts which rate ids survive hide-rates-when-free, how woocommerce_package_rates handles a filter that errors, and when the package hash invalidates the rate cache. The cost is not the subject there.

Fixed costs reach the cart total in several places: legacy/unit-tests/cart/cart.php configures flat rates at 9.59, 8.05 and 4.12 and asserts the resulting totals, and Cart.php::test_get_item asserts total_shipping at '1000' from FixtureData::shipping_add_flat_rate().

Not covered

Everything below was searched for by the identifiers the behaviour uses rather than by test name, across the whole tree except e2e.

  • Shipping classes, entirely. class_cost and no_class_cost appear nowhere in any test. So the chain from class_cost_<term_id> to the legacy class_cost_<slug> to no_class_cost is unguarded, and so is type = class summing every matched class against type = order taking only the highest
  • A blank cost. No flat rate test sets 'cost' => ''; the only blank costs in the tree belong to pickup location fixtures. So a blank cost emitting no rate at all, while '0' emits a rate priced zero, is unpinned
  • [qty] through a real rate. It reaches only persistence: class-wc-rest-shipping-zones-v4-controller-tests.php::test_non_numeric_cost_handling saves '10 + [qty] * 2' and asserts the string round-trips through REST, never that it evaluates. Otherwise [qty] appears only at the evaluate_cost() level with the quantity passed in by hand
  • [cost] as a literal placeholder. The string appears in no test. The fee base is pinned indirectly by the rounding test above, but [cost] itself is not
  • The rate's own price. No test asserts it. $rate->cost appears only in helpers that build an order shipping item from a rate, and the shipping_rates assertions in Cart.php check destination only. Every cost assertion in the tree lands on a cart or order total instead
  • [fee percent="[weight]"], which works only because [weight] is expanded as raw text before do_shortcode() runs and would break silently on a reordering. The nearest existing test is test_evaluate_cost_weight_does_not_change_fee_base, which uses '[fee percent="10"] + [weight]' and pins the opposite point, that the fee stays based on cost

Conventions

Pin outcomes for core rules. Where core deliberately leaves an extension point open, pin that the seam is honoured rather than freezing the outcome, otherwise the test closes the door the seam exists for. The seam in scope here is woocommerce_evaluate_shipping_cost_args.

Where a test replaces or overlaps existing coverage, show it is equivalent by mutation: break the rule in the source, confirm the new test fails, restore.

Integration and unit tests only, no E2E.