Shipping tests: whether shipping applies
What this covers
The merchant switches shipping off, or the cart holds nothing that ships, and the shopper should not meet a step that does not apply. PHP and client.
Expected behaviour
- A digital-only cart has no shipping step
- A mixed cart does
- Shipping switched off removes the step and the order can still be placed
- With no methods defined at all there is no step
- Switching from guest to logged in recalculates, since the package carries the user
- With at least one instance present, enabled or not,
needs_shippingis true for a cart with a physical item - With no instances at all and pickup off, it is false
ship_to_countries = disabledis the supported way to switch shipping off, and it makeswc_shipping_enabled()false- A destination outside the ship-to list leaves pickup as the only option, because
is_package_shippable()is false and only pickup method ids are asked for rates
Where it breaks
- A product marked virtual after it is already in the cart
- Every method in the zone disabled, which is not the same as no methods at all
- A cart that empties while the shipping step is open
One row pins behaviour that looks like a bug and is not
needs_shipping() calls wc_get_shipping_method_count( true ), whose first parameter is $include_legacy and not $enabled_only, so instances that exist but are disabled still count and needs_shipping stays true. woocommerce#56507 proposed changing it and was closed deliberately. Mike Jolley: "I'd worry that changing this behaviour would have implications for merchants using some form of conditional shipping system. e.g. if they had something which disabled methods based on user type, would that allow them to place order? ... Also note, if the merchant really wants to disable shipping, there is a separate option to do this. For these reasons I would opt to leave it as is." Thomas Roberts, who filed it, agreed and closed it.
The test asserts the current behaviour and carries that reasoning in its docblock. Do not test the "Expected behavior" section of woocommerce#56507's body; the comments supersede it.
Already covered
Checkout.php::test_virtual_product_post_data, Cart.php::test_update_customer_virtual_cart and Checkout.php::test_shipping_address_is_ignored_when_cart_does_not_need_shipping cover the digital-cart path. The show_shipping family in class-wc-cart-test.php covers the classic side. Checkout.php::test_local_pickup_country_validation pins that a shipping country which is not enabled still checks out when pickup_location:0 is the chosen method, and test_local_pickup_invalid_billing_country pins that billing country validation still applies to a pickup order.
Not covered
- The rule that an instance which exists but is disabled still counts
ship_to_countries = disabledmakingwc_shipping_enabled()false- The destination-outside-ship-to case at the rate level, where
is_package_shippable()is false and only pickup method ids are asked for rates, so the result is pickup-only rather than empty - The difference between every method in a zone being disabled and no methods existing at all
- A product marked virtual after it is already in the cart
Conventions
The seam in scope is woocommerce_product_needs_shipping. Pin that it is honoured, not that the outcome is fixed.
Integration and unit tests only, no E2E.
Source: woocommerce/woocommerce