This snippet sets a different minimum delivery amount for each city in FoodMaster (formerly WooFood). Add it to your child theme’s functions.php file, or to a code snippets plugin if you would rather not edit theme files.
Set the minimum amount per city
Replace the city names and amounts in the $minimum array with your own. The snippet reads the customer’s billing city and removes spaces before it looks the city up.
Updated for current FoodMaster versions: the code below uses the foodmaster_ names. Older installs that still run WooFood used the woofood_ prefix, so on those change foodmaster_ back to woofood_.
add_filter("foodmaster_minimum_amount_delivery_filter", "wpslash_minimum_delivery_hook_on_message_per_city", 10, 1);
function wpslash_minimum_delivery_hook_on_message_per_city($min_amount) {
global $woocommerce;
$minimum = array(
"Niefern" => 8,
"Niefern-Öschelbronn" => 8,
"Pinache" => 8,
"Wiernsheim" => 12,
"Neubärental" => 12,
"Großglattbach" => 12,
"PF-Eutingen" => 13,
"Enzberg" => 13,
"Lormersheim" => 13,
"Mönsheim" => 15,
"Wimsheim" => 15,
"PF-Mäurach" => 15,
"PF-Hagenschieß" => 15,
"Kieselbronn" => 15,
"Dürrmenz" => 15,
"Serres" => 15,
"Mühlacker" => 17,
"Mühlacker Mühlhausen" => 17,
"Sengach" => 18,
"Iptingen" => 18,
"PF-Oststadt" => 22,
"Weissach" => 22,
"Friolzheim" => 22,
"PF-Innenstadt" => 25,
"PF-Südstadt" => 25,
"Buckenberg" => 25,
"Haidach" => 25,
"Nußdorf" => 25,
"Lienzingen" => 25,
"Ötisheim" => 25,
"Corres" => 25,
"Öt-Erlenbach" => 25,
"Tiefenbronn" => 25,
"PF-Nordstadt" => 30,
"Bauschlott" => 30,
"Göbrichen" => 30,
"Ölbronn-Dürrn" => 30,
"Eberdingen" => 30,
"Pf-Brötzingen" => 35,
"Pforzheim" => 35,
"Weststadt" => 35,
"Maihälden" => 35,
"Neulingen" => 35
);
$city = str_replace(" ", "", $woocommerce->customer->get_billing_city());
if (array_key_exists($city, $minimum)) {
return $minimum[$city];
}
return $min_amount;
}
How the snippet works
The function hooks into foodmaster_minimum_amount_delivery_filter. It takes the billing city, strips the spaces, and returns the amount stored for that city. If the city is not in the array, it returns the minimum amount it was given.
Add a city dropdown to the checkout
WooCommerce lets customers type the city by hand, so the value may not match a key in the array. This second snippet turns the billing city field into a dropdown built from the same list.
add_filter('woocommerce_checkout_fields', function($fields) {
$minimum = array(
"Niefern" => 8,
"Niefern-Öschelbronn" => 8,
"Pinache" => 8,
"Wiernsheim" => 12,
"Neubärental" => 12,
"Großglattbach" => 12,
"PF-Eutingen" => 13,
"Enzberg" => 13,
"Lormersheim" => 13,
"Mönsheim" => 15,
"Wimsheim" => 15,
"PF-Mäurach" => 15,
"PF-Hagenschieß" => 15,
"Kieselbronn" => 15,
"Dürrmenz" => 15,
"Serres" => 15,
"Mühlacker" => 17,
"Mühlacker Mühlhausen" => 17,
"Sengach" => 18,
"Iptingen" => 18,
"PF-Oststadt" => 22,
"Weissach" => 22,
"Friolzheim" => 22,
"PF-Innenstadt" => 25,
"PF-Südstadt" => 25,
"Buckenberg" => 25,
"Haidach" => 25,
"Nußdorf" => 25,
"Lienzingen" => 25,
"Ötisheim" => 25,
"Corres" => 25,
"Öt-Erlenbach" => 25,
"Tiefenbronn" => 25,
"PF-Nordstadt" => 30,
"Bauschlott" => 30,
"Göbrichen" => 30,
"Ölbronn-Dürrn" => 30,
"Eberdingen" => 30,
"Pf-Brötzingen" => 35,
"Pforzheim" => 35,
"Weststadt" => 35,
"Maihälden" => 35,
"Neulingen" => 35
);
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = array_merge(array('' => 'Select your city'), $minimum);
return $fields;
});
To check it worked, add an item to the cart, choose a city in the dropdown and go to the checkout. The order should be blocked until the total reaches the amount for that city.
FoodMaster (formerly WooFood)
The first snippet uses a filter from FoodMaster (formerly WooFood), the restaurant ordering plugin for WooCommerce. The second uses the standard WooCommerce woocommerce_checkout_fields filter. You can get FoodMaster here.
