0

I would like the following code (or something close to it) to only apply if the customers geolocation is from the UK? I've tried a few different options but have been unable to get it working when I test from another country.


function free_shipping_cart_notice() {

    $min_amount = 30;

    // Subtotal inc. Tax excl. Shipping
    $current = WC()->cart->subtotal;

    if ( $current < $min_amount ) {
        $added_text = esc_html__('You will have FREE shipping if you add ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' more in your order!', 'woocommerce' );
        $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
        $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
        wc_print_notice( $notice, 'notice' );
    } else {
        wc_print_notice( 'Congrats! You have free shipping with more than £30 in your order', 'notice' );
    }

}
add_action( 'woocommerce_before_cart', 'free_shipping_cart_notice' );

Mike Devitt
  • 115
  • 1
  • 6

1 Answers1

0

Ok, have tried another method and have got this working.

//free shipping qualification UK only
add_action( 'woocommerce_before_cart', 'md_free_shipping_cart_notice' );

function md_free_shipping_cart_notice() {
    $threshold = 25;
    $current = WC()->cart->subtotal;
    $billing_country = WC()->customer->get_billing_country();
    if ( $current < $threshold && WC()->customer->get_billing_country() == 'GB' ) {
        $added_text = esc_html__('You will have FREE shipping if you add ', 'woocommerce' ) . wc_price( $threshold - $current ) . esc_html__(' more in your order!', 'woocommerce' );
        $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
        $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
        wc_print_notice( $notice, 'notice' );
    } elseif ( $current > $threshold && WC()->customer->get_billing_country() == 'GB' ) {
        wc_print_notice( 'Congrats! You have free shipping with more than £25 in your order', 'notice' );
    }

}

Hope this helps someone else

Mike Devitt
  • 115
  • 1
  • 6