Tax Region Resolution Map
Resolves a tax region from shipping country, state, postal prefix, and fallback rules.
Purpose
Resolves a tax region from shipping country, state, postal prefix, and fallback rules.
Snippet details
ContextEcommerceLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Tax Region Resolution Map.
*
* Purpose:
* Resolves a tax region from shipping country, state, postal prefix, and fallback rules.
*
* @param array $address Shipping address.
* @param array $rules Tax region rules.
* @return array Resolved tax region.
*/
function ogSnippetTaxRegionResolutionMap(array $address, array $rules): array {
$country = strtoupper((string) $address['country']);
$state = strtoupper((string) $address['state']);
$postal = strtoupper((string) $address['postal']);
foreach ($rules as $rule) {
if ((string) $rule['country'] !== $country) {
continue;
}
if (isset($rule['state']) && (string) $rule['state'] !== $state) {
continue;
}
if (isset($rule['postal_prefix']) && strpos($postal, (string) $rule['postal_prefix']) !== 0) {
continue;
}
return array('region' => (string) $rule['region'], 'matched' => true);
}
return array('region' => 'default', 'matched' => false);
}
$tax_region = ogSnippetTaxRegionResolutionMap(array('country' => 'US', 'state' => 'NY', 'postal' => '10001'), array(array('country' => 'US', 'state' => 'NY', 'region' => 'new-york')));
echo $tax_region['region'];