Уведомление о доставке Woocommerce на основе геолокации - PullRequest
0 голосов
/ 03 ноября 2019

Хотелось бы, чтобы следующий код (или что-то похожее на него) применялся только в том случае, если геолокация клиентов происходит из Великобритании? Я пробовал несколько разных вариантов, но не смог заставить его работать, когда я тестировал из другой страны.


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' );

1 Ответ

0 голосов
/ 04 ноября 2019

Хорошо, попробовал другой метод и получил это работает.

//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' );
    }

}

Надеюсь, это поможет кому-то еще

...