Set Different Minimum Delivery amount per City from Predefined Cities Select Field

July 3, 2021

TutorialsWooFood
woocommerce food orderingwordpress food ordering plugin
add_filter("woofood_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();
	

	$minimum["Niefern"] = 8;
	$minimum["Niefern-Öschelbronn"] = 8;
	$minimum["Pinache"] = 8;
	$minimum["Wiernsheim"] = 12;
	$minimum["Neubärental"] = 12;
	$minimum["Großglattbach"] = 12;
	$minimum["PF-Eutingen"] = 13;
	$minimum["Enzberg"] = 13;
	$minimum["Lormersheim"] = 13;
	$minimum["Mönsheim"] = 15;
	$minimum["Wimsheim"] = 15;
	$minimum["PF-Mäurach"] = 15;
	$minimum["PF-Hagenschieß"] = 15;
	$minimum["Kieselbronn"] = 15;
	$minimum["Dürrmenz"] = 15;
	$minimum["Serres"] = 15;
	$minimum["Mühlacker"] = 17;
	$minimum["Mühlacker Mühlhausen"] = 17;
	$minimum["Sengach"] = 18;
	$minimum["Iptingen"] = 18;
	$minimum["PF-Oststadt"] = 22;
	$minimum["Weissach"] = 22;
	$minimum["Friolzheim"] = 22;
	$minimum["PF-Innenstadt"] = 25;
	$minimum["PF-Südstadt"] = 25;
	$minimum["Buckenberg"] = 25;
	$minimum["Haidach"] = 25;
	$minimum["Nußdorf"] = 25;
	$minimum["Lienzingen"] = 25;
	$minimum["Ötisheim"] = 25;
	$minimum["Corres"] = 25;
	$minimum["Öt-Erlenbach"] = 25;
	$minimum["Tiefenbronn"] = 25;
	$minimum["PF-Nordstadt"] = 30;
	$minimum["Bauschlott"] = 30;
	$minimum["Göbrichen"] = 30;
	$minimum["Ölbronn-Dürrn"] = 30;
	$minimum["Eberdingen"] = 30;
	$minimum["Pf-Brötzingen"] = 35;
	$minimum["Pforzheim"] = 35;
	$minimum["Weststadt"] = 35;
	$minimum["Maihälden"] = 35;
	$minimum["Neulingen"] = 35;




    $city = str_replace(" ", "", $woocommerce->customer->get_billing_city());	
	if(array_key_exists($city, $minimum))
	{
			return $minimum[$city];

	}
	else{
		return $min_amount;
	}
	
	

}



add_filter('woocommerce_checkout_fields', function($fields) {

	$minimum = array();
	

	$minimum["Niefern"] = 8;
	$minimum["Niefern-Öschelbronn"] = 8;
	$minimum["Pinache"] = 8;
	$minimum["Wiernsheim"] = 12;
	$minimum["Neubärental"] = 12;
	$minimum["Großglattbach"] = 12;
	$minimum["PF-Eutingen"] = 13;
	$minimum["Enzberg"] = 13;
	$minimum["Lormersheim"] = 13;
	$minimum["Mönsheim"] = 15;
	$minimum["Wimsheim"] = 15;
	$minimum["PF-Mäurach"] = 15;
	$minimum["PF-Hagenschieß"] = 15;
	$minimum["Kieselbronn"] = 15;
	$minimum["Dürrmenz"] = 15;
	$minimum["Serres"] = 15;
	$minimum["Mühlacker"] = 17;
	$minimum["Mühlacker Mühlhausen"] = 17;
	$minimum["Sengach"] = 18;
	$minimum["Iptingen"] = 18;
	$minimum["PF-Oststadt"] = 22;
	$minimum["Weissach"] = 22;
	$minimum["Friolzheim"] = 22;
	$minimum["PF-Innenstadt"] = 25;
	$minimum["PF-Südstadt"] = 25;
	$minimum["Buckenberg"] = 25;
	$minimum["Haidach"] = 25;
	$minimum["Nußdorf"] = 25;
	$minimum["Lienzingen"] = 25;
	$minimum["Ötisheim"] = 25;
	$minimum["Corres"] = 25;
	$minimum["Öt-Erlenbach"] = 25;
	$minimum["Tiefenbronn"] = 25;
	$minimum["PF-Nordstadt"] = 30;
	$minimum["Bauschlott"] = 30;
	$minimum["Göbrichen"] = 30;
	$minimum["Ölbronn-Dürrn"] = 30;
	$minimum["Eberdingen"] = 30;
	$minimum["Pf-Brötzingen"] = 35;
	$minimum["Pforzheim"] = 35;
	$minimum["Weststadt"] = 35;
	$minimum["Maihälden"] = 35;
	$minimum["Neulingen"] = 35;
	 $cities = array();
	foreach($minimum as $city=> $min_value)
	{
			 $cities[$city] = $city;

	}
	$fields['billing']['billing_city']['type'] = 'select';
	$fields['billing']['billing_city']['options'] = $cities;

	return $fields;
});

add_action( 'woocommerce_checkout_process', 'woofood_custom_hook_minimum_per_city', 9999999 );
 function woofood_custom_hook_minimum_per_city() {
 global $woocommerce;
	$minimum = array();
	

	$minimum["Niefern"] = 8;
	$minimum["Niefern-Öschelbronn"] = 8;
	$minimum["Pinache"] = 8;
	$minimum["Wiernsheim"] = 12;
	$minimum["Neubärental"] = 12;
	$minimum["Großglattbach"] = 12;
	$minimum["PF-Eutingen"] = 13;
	$minimum["Enzberg"] = 13;
	$minimum["Lormersheim"] = 13;
	$minimum["Mönsheim"] = 15;
	$minimum["Wimsheim"] = 15;
	$minimum["PF-Mäurach"] = 15;
	$minimum["PF-Hagenschieß"] = 15;
	$minimum["Kieselbronn"] = 15;
	$minimum["Dürrmenz"] = 15;
	$minimum["Serres"] = 15;
	$minimum["Mühlacker"] = 17;
	$minimum["Mühlacker Mühlhausen"] = 17;
	$minimum["Sengach"] = 18;
	$minimum["Iptingen"] = 18;
	$minimum["PF-Oststadt"] = 22;
	$minimum["Weissach"] = 22;
	$minimum["Friolzheim"] = 22;
	$minimum["PF-Innenstadt"] = 25;
	$minimum["PF-Südstadt"] = 25;
	$minimum["Buckenberg"] = 25;
	$minimum["Haidach"] = 25;
	$minimum["Nußdorf"] = 25;
	$minimum["Lienzingen"] = 25;
	$minimum["Ötisheim"] = 25;
	$minimum["Corres"] = 25;
	$minimum["Öt-Erlenbach"] = 25;
	$minimum["Tiefenbronn"] = 25;
	$minimum["PF-Nordstadt"] = 30;
	$minimum["Bauschlott"] = 30;
	$minimum["Göbrichen"] = 30;
	$minimum["Ölbronn-Dürrn"] = 30;
	$minimum["Eberdingen"] = 30;
	$minimum["Pf-Brötzingen"] = 35;
	$minimum["Pforzheim"] = 35;
	$minimum["Weststadt"] = 35;
	$minimum["Maihälden"] = 35;
	$minimum["Neulingen"] = 35;






    if (isset($_POST["billing_city"])) {


	if(array_key_exists($_POST["billing_city"], $minimum))
	{
		$city = $_POST["billing_city"];
			if(WC()->cart->subtotal < $minimum[$city] )
			{
				 wc_add_notice( 
                sprintf(  __( 'You must have an order with a minimum of %s to place your order, your current order total is %s.', 'woofood-plugin' ) , 
                    wc_price( $minimum[$city] ), 
                    wc_price( WC()->cart->subtotal )
                ), 'error' 
            );

			}

	}


      

    }
}