It's actually a bit more complicated... The customer, billing, and shipping addresses are not stored in the "orders" database table as "pointers" to an entry in a customers "address_book". Instead the data is extracted from the "address_book" when the order is created and only the text portions are stored directly with the order.
This ensures the address information associated from an order is kept separate from the customer's "address book". This makes sure the order history is always accurate. It also allows for county / state data not present in the database to be used (such as when state dropdowns are disabled). As a side effect one can also edit addresses associated with a specific order directly without affecting anything else (editing an existing order requires a third party Zen Cart plugin).
This means we cannot use the "address_book" and will need read the addresses associated with the order. These do not contain the "country_id" or "zone_id" for the address. We also need to take into consideration some "country" and "state" (zone) data may not exist in the database and fall back to simply displaying the raw data from the order (when they cannot be found in the database).
The function zen_address_format already takes both of these scenarios into account. If one includes the "country_id" and "zone_id" in the address, it will display the "state abbr" (zone_code) instead of the full "state" (state name). If one does not include these, the passed "country" and "state" are displayed as-is.
Code Follows
I've had the need to check / validate / lookup both country and zone information in the past (largely for Admin New Orders and Edit Orders). The following changes should effectively alter the display of your invoices without affecting any other pages or altering the data stored in the database (so should not impact any other Zen Cart code or 3rd party modules).
First we need to create a function to "search" for the country_id and zone_id. When found the function should add them to the address. For example one could add the following function (in "/admin/includes/functions/extra_functions/use_state_abbr.php"):
Code:
function add_zone_info_to_address($address) {
global $db;
$query = 'SELECT `countries_id` FROM `' . TABLE_COUNTRIES . '` ';
if(strlen($address['country']) == 2) $query .= 'WHERE `countries_iso_code_2`= \'' . $db->prepare_input($address['country']) . '\'';
else if(strlen($address['country']) == 3) $query .= 'WHERE `countries_iso_code_3`=\'' . $db->prepare_input($address['country']) . '\'';
else $query .= 'WHERE `countries_name`=\'' . $db->prepare_input($address['country']) . '\'';
$query = $db->Execute($query . ' LIMIT 1');
if(!$query->EOF) {
$address['country_id'] = $query->fields['countries_id'];
$query = $db->Execute(
'SELECT `zone_id` FROM `' . TABLE_ZONES . '` ' .
'WHERE `zone_country_id`=\'' . $address['country_id'] .
'\' AND (`zone_code`=\'' . $db->prepare_input($address['state']) .
'\' OR `zone_name`=\''. $db->prepare_input($address['state']) . '\') LIMIT 1'
);
if(!$query->EOF) {
$address['zone_id'] = $query->fields['zone_id'];
}
}
return $address;
}
Second when sending the address to zen_address_format, we need to wrap the address with the new function. In the stock ZC 1.5.1 copy of "/admin/invoice.php" these are on line 91, 100, and 117. For example on line 91:
Code:
<td class="main"><?php echo zen_address_format($order->customer['format_id'], add_zone_info_to_address($order->customer), 1, '', '<br>'); ?></td>
This should produce the desired result with the least number of changes to core Zen Cart files :)
Bookmarks