Skip to content

Commit 4410bf4

Browse files
Merge pull request #803 from arnelap/Address-fiield-fallback-snippet
Code snippet: fallback-values-for-address.php
2 parents d0ebdb6 + 7789730 commit 4410bf4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Mailchimp always expects a full and complete address when you use a field of type "address"
5+
*
6+
* This snippet will allow you to submit a partial address and fill missing fields with fallback values.
7+
* Fill sensible fallback data for your region, especially ZIP might expect a different value depending on your locale.
8+
*/
9+
10+
add_filter('mc4wp_format_field_value', function ($value, $field_type) {
11+
if ($field_type === 'address') {
12+
$value = is_array($value) ? $value : [];
13+
14+
$fallbacks = [
15+
'addr1' => 'N/A',
16+
'city' => 'N/A',
17+
'state' => 'N/A',
18+
'zip' => '00000',
19+
];
20+
21+
// Fill missing values from fallbacks
22+
foreach ($fallbacks as $k => $fallback) {
23+
$value[$k] = isset($value[$k]) && $value[$k] !== '' ? $value[$k] : $fallback;
24+
}
25+
}
26+
return $value;
27+
}, 10, 2);

0 commit comments

Comments
 (0)