Automattic\WooCommerce\Blocks\Shipping
ShippingController::filter_order_tax_location
Filter the tax location for an order so local pickup orders are taxed at the chosen pickup location's address.
An order's stored shipping/billing address is not the correct tax location for local pickup: the customer collects from the store, so tax must be based on the pickup location. WC_Abstract_Order::get_tax_location() already forces the shop base address for local pickup orders; this filter refines that to the specific pickup location's address, which is captured on the shipping line item at purchase time.
This is the order-side counterpart to {@see self::filter_taxable_address()}, which performs the equivalent override for the cart via the customer's taxable address. Resolving the address from the order (rather than the customer session) keeps the result correct for any order tax read, including admin recalculation, background jobs, and multiple orders handled within a single request.
Method of the class: ShippingController{}
Hooks from the method
Returns
Array.
Usage
$ShippingController = new ShippingController(); $ShippingController->filter_order_tax_location( $location, $order );
- $location(array) (required)
- Tax location with
'country','state','postcode'and'city'keys. - $order(WC_Abstract_Order) (required)
- Order the tax location is being resolved for.
Changelog
| Since 11.0.0 | Introduced. |
ShippingController::filter_order_tax_location() ShippingController::filter order tax location code WC 11.0.1
public function filter_order_tax_location( $location, $order ) {
if ( ! $order instanceof \WC_Abstract_Order ) {
return $location;
}
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
if ( true !== apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) ) {
return $location;
}
// Use the same canonical local pickup method list that WC_Abstract_Order::get_tax_location() uses to force
// the base address. Relying on the currently-registered methods (LocalPickupUtils::get_local_pickup_method_ids())
// would miss legacy or deregistered methods on existing orders, leaving them taxed at the store base instead.
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Documented in WC_Abstract_Order::get_tax_location().
$local_pickup_method_ids = apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) );
foreach ( $order->get_shipping_methods() as $shipping_method ) {
if ( ! in_array( $shipping_method->get_method_id(), $local_pickup_method_ids, true ) ) {
continue;
}
$pickup_address = $shipping_method->get_meta( '_pickup_location_address' );
if ( is_array( $pickup_address ) && ! empty( $pickup_address['country'] ) ) {
return array(
'country' => $pickup_address['country'],
'state' => $pickup_address['state'] ?? '',
'postcode' => $pickup_address['postcode'] ?? '',
'city' => $pickup_address['city'] ?? '',
);
}
}
return $location;
}