duanfu3634 2019-04-29 09:11
浏览 38
已采纳

保留报价开票和发货国家ELSE根据IP(地理位置)分配位置

Information:

WooCommerce version: 3.6.2

WordPress version: 5.1.1

YITH WooCommerce Request A Quote Premium by YITH – 2.1.7


Objective:

If a customer is checking out using a Quote then retain the Billing and Shipping country set with the Quote, else restrict the country using Geolocation

If it helps, the Quote url sent to the customer looks like this: mysite/request-quote/?request_quote=330&status=accepted&raq_nonce=ad5c139f3d7a59993a4ff679d84de3a3&lang


My Code attempt

// GeoIP Country
add_filter( 'woocommerce_checkout_fields' , 'vals_woo_checkout_country' );
function vals_woo_checkout_country( $fields ) {
//check if Billing & Shipping is ALREADY set, if yes use it
if (isset($_POST['billing_country'])) {
$billing_country  = WC()->customer->get_country();
$shipping_country = ! empty( WC()->customer->get_shipping_country()  )  ? WC()->customer->get_shipping_country() : $billing_country;
}
//If not set then use geolocation to set
else
//Only in Checkout and in Order Pay
if ( (is_checkout()) && (is_wc_endpoint_url('order-pay') )) {
            $geoData = WC_Geolocation::geolocate_ip();
            $Geocountries = WC()->countries->get_countries();
            $fields['billing']['billing_country'] = array(
                'type' => 'select',
                'label'     => __('Country', 'woocommerce'),
                'options' => array(
                    $geoData['country'] => $Geocountries[$geoData['country']]
                ),
                'class' => array(
                    'form-row-wide',
                    'address-field',
                    'update_totals_on_change'
                )
            );
            $fields['shipping']['shipping_country'] = array(
                'type' => 'select',
                'label'     => __('Country', 'woocommerce'),
                'options' => array(
                    $geoData['country'] => $Geocountries[$geoData['country']]
                ),
                'class' => array(
                    'form-row-wide',
                    'address-field',
                    'update_totals_on_change'
                )
            );
}
            return $fields;
        }
function remove_checkout_optional_fields_label_script() {
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;

    $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
    ?>
    <script>
    jQuery(function($){
        // On "update" checkout form event
        $(document.body).on('update_checkout', function(){
            $('#billing_country_field label > .optional').remove();
    $('.woocommerce-checkout #billing_country_field').css('pointer-events', 'none');
            $('#shipping_country_field label > .optional').remove();
    $('.woocommerce-checkout #shipping_country_field').css('pointer-events', 'none');
        });
    });
    </script>
    <?php
}
add_filter( 'wp_footer' , 'remove_checkout_optional_fields_label_script' );
//end code

Update 03/May/2019

This code does NOT work.

Any help on this is welcome.

  • 写回答

2条回答 默认 最新

  • dongza1708 2019-05-06 05:57
    关注

    I finally thought this over and have a different solution.

    Part 1 - Settings Under Woocommerce -> Settings -> General -> Default customer location = Geolocate Woocommerce setting

    Part 2 - Code Code goes in function.php file of your active child theme (or active theme).

    function remove_checkout_optional_fields_label_script() {
        // Only on checkout page
        if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        ?>
        <script>
        jQuery(function($){
            // On "update" checkout form event remove option to select countries
            $(document.body).on('update_checkout', function(){
                $('#billing_country_field label > .optional').remove();
        $('.woocommerce-checkout #billing_country_field').css('pointer-events', 'none');
                $('#shipping_country_field label > .optional').remove();
        $('.woocommerce-checkout #shipping_country_field').css('pointer-events', 'none');
            });
        });
        </script>
        <?php
    }
    add_filter( 'wp_footer' , 'remove_checkout_optional_fields_label_script' );
    

    This works for me for now. I would really be grateful if anyone could suggest a better solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题