Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 350 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strict to 1 country only in checkout page with wordpress and woocommerce

#1
I am using wordpress and woocommerce. In checkout page, how do I restrict to only 1 country only? Say Australia.

[![enter image description here][1]][1]


[1]:
Reply

#2
Just override the class by 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' );

This will help you to show only 1 country in dropdown. Add this code to functions.php in theme.
Reply

#3
Hello you can restrict to only one country by plugin settings

[![woocommerce plugin settings][1]][1]


[1]:


you can find in **Woocommerce**->**Settings**-> Genral tab

Reply

#4
Also maybe you want to sell to several countries but also you want to show ONLY the country where the user is connecting from (geolocated from IP address). Thus, in this way, a french user will only see France in the country dropdown, an australian user will only see Australia in the country dropdown and so on... Here is the 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 );
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through