【问题标题】:Woocommerce Shipping notification based on Geolocation基于地理位置的 Woocommerce Shipping 通知
【发布时间】:2019-11-03 11:07:25
【问题描述】:

我希望以下代码(或类似代码)仅适用于客户地理位置来自英国的情况?我尝试了几种不同的选项,但是当我在另一个国家/地区进行测试时无法使其正常工作。


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

【问题讨论】:

    标签: php woocommerce shipping-method


    【解决方案1】:

    好的,已经尝试了另一种方法并且已经成功了。

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

    希望这对其他人有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2021-06-14
      • 2015-05-11
      • 2016-12-29
      • 1970-01-01
      • 2018-11-07
      相关资源
      最近更新 更多