Ich nutze WordPress und WooCommerce. Wie kann ich auf der Checkout-Seite auf nur 1 Land beschränken? Sprich Australien.
Streng auf 1 Land nur auf der Checkout-Seite mit WordPress und Woocommerce
Hallo, Sie können durch Plugin-Einstellungen auf nur ein Land beschränken
findest du darin WooCommerce->Einstellungen-> Reiter Allgemein
Überschreiben Sie einfach die Klasse per Hook,
function woo_override_checkout_fields_billing( $fields ) {
$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('My New Country List', 'woocommerce'),
'options' => array('AU' => 'Australia')
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields_billing' );
function woo_override_checkout_fields_shipping( $fields ) {
$fields['shipping']['shipping_country'] = array(
'type' => 'select',
'label' => __('My New Country List', 'woocommerce'),
'options' => array('AU' => 'Australia')
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields_shipping' );
Dies hilft Ihnen, nur 1 Land in der Dropdown-Liste anzuzeigen. Fügen Sie diesen Code zu functions.php im Design hinzu.
hormigaz
Vielleicht möchten Sie auch in mehrere Länder verkaufen, aber auch NUR das Land anzeigen, aus dem der Benutzer eine Verbindung herstellt (geolokalisierte IP-Adresse). Auf diese Weise sieht ein französischer Benutzer also nur Frankreich im Länder-Dropdown, ein australischer Benutzer sieht nur Australien im Länder-Dropdown und so weiter … Hier ist der Code:
/**
* @param array $countries
* @return array
*/
function custom_update_allowed_countries( $countries ) {
// Only on frontend
if( is_admin() ) return $countries;
if( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();
if ( isset( $location['country'] ) ) {
$countryCode = $location['country'];
} else {
// If there is no country, then return allowed countries
return $countries;
}
} else {
// If you can't geolocate user country by IP, then return allowed countries
return $countries;
}
// If everything went ok then I filter user country in the allowed countries array
$user_country_code_array = array( $countryCode );
$intersect_countries = array_intersect_key( $countries, array_flip( $user_country_code_array ) );
return $intersect_countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'custom_update_allowed_countries', 30, 1 );
-
Sie haben vergessen, $ zu countryCode bei $user_country_code_array = array( countryCode );
– Adrian González
30. Oktober 2020 um 15:54 Uhr