Skip to content

Instantly share code, notes, and snippets.

@noellesteegs
Last active September 12, 2023 12:13
Show Gist options
  • Save noellesteegs/98b77b51f0ea18ffedc277c1094c3501 to your computer and use it in GitHub Desktop.
Save noellesteegs/98b77b51f0ea18ffedc277c1094c3501 to your computer and use it in GitHub Desktop.
"You're only x away from free shipping" notice
function na_woo_free_shipping_notice() {
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { // Check whether Woo is active
$location = WC_Geolocation::geolocate_ip(); // Get location data via MaxMind Geolocation
$country = $location['country']; // Get the country from MaxMind data
switch ( $country ) { // Check what the country is
case "BE": // If it's Belgium...
$min_amount = 100; /// ...then apply this amount...
break;
case "DE": // ...etc.
$min_amount = 150;
break;
default: // Standard minimum
$min_amount = 60;
}
$current_subtotal = WC()->cart->subtotal; // Check the cart's subtotal
if ( $current_subtotal < $min_amount ) { // If the subtotal is less than the minimum...
$message = 'You're only ' . wc_price( $min_amount - $current_subtotal ) . ' away from free shipping.'; // Message with calculation and correct formatting
echo '<p class="na-woo-free-shipping-notice">' . $message . '</p>'; // ...show the text as a paragraph
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment