Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function - Disable Checkout for some Countries and States
<?php
/*
* Function - Disable Checkout for some Countries and States
*/
function wc_checkout_validation() {
$shipping_country = ! empty( $_POST['shipping_country'] ) ? $_POST['shipping_country'] : $_POST['billing_country'];
$shipping_state = ! empty( $_POST['shipping_state'] ) ? $_POST['shipping_state'] : $_POST['billing_state'];
// Add here States : More example look here -> wp-content/plugins/woocommerce/i18n/states/
$excluded_states = array(
'SP',
'DF',
'RS',
);
// Verification if you are in Brazil : More example look here -> wp-content/plugins/woocommerce/i18n/countries.php
if ( 'BR' != $shipping_country ) {
wc_add_notice( __( 'Desculpem-nos, infelizmente não vendemos para fora do Brasil.', 'odin' ), 'error' );
}
// Verification Brazilian States
if ( in_array( $shipping_state, $excluded_states ) ) {
// Here you generate State Label for your Alert, if you use a different country. Please, specific below your correct country.
$states = WC()->countries->get_states( 'BR' );
$state = $states[$shipping_state];
wc_add_notice( sprintf( __( 'Desculpem-nos, infelizmente não realizamos entregas para <strong>%s</strong>.', 'odin' ), $state ), 'error' );
}
}
add_action( 'woocommerce_checkout_process', 'wc_checkout_validation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment