【问题标题】:Checkout - Limit the Countries in Billing & Shipping based on Geolocation结帐 - 根据地理位置限制计费和运输中的国家/地区
【发布时间】:2019-07-08 02:24:45
【问题描述】:

WordPress 版本:5.0.3 WooCommerce 版本:3.5.4

我有一家多国商店,在结账时,我想使用地理位置将国家/地区计费和运送限制在用户 IP 地址位置。

借助来自Shipping Location based on IP (Geolocation) 的惊人代码,我设法做到了这一点,并使用来自Remove “(optional)” text from checkout fields in Woocommerce 3.4+ 的代码删除(可选)文本

我的最终代码是:

        function wpse_287199_woo_checkout_country( $fields ) {
            $geoData = WC_Geolocation::geolocate_ip();
            $countries = WC()->countries->get_countries();

            $fields['billing']['billing_country'] = array(
                'type' => 'select',
                'label'     => __('Country', 'woocommerce'),
                'options' => array(
                    $geoData['country'] => $countries[$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'] => $countries[$geoData['country']]
                ),
                'class' => array(
                    'form-row-wide',
                    'address-field',
                    'update_totals_on_change'
                )
            );

            return $fields;
        }
        add_filter( 'woocommerce_checkout_fields' , 'wpse_287199_woo_checkout_country' );
        add_filter( 'wp_footer' , 'remove_checkout_optional_fields_label_script' );
        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();
                    $('#shipping_country_field label > .optional').remove();
                });
            });
            </script>
            <?php
        }

但是,使用此代码,客户会看到带有不可选择的选择框的字段。这会惹恼客户。请看下面的截图:

有人可以指导我对代码进行更改,以便删除选择选项。客户应该会看到国家/地区字段,如下面的屏幕截图所示:

非常感谢

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    好的,我无法完全得到我希望达到的目标,但得到了一些接近

    我的最终代码:

    /** Restrict Change in Checkout Country based on Geo IP (geolocation)*/
    function wpse_287199_woo_checkout_country( $fields ) {
        $geoData = WC_Geolocation::geolocate_ip();
        $countries = WC()->countries->get_countries();
    $fields['billing']['billing_country'] = array(
            'type' => 'select',
            'label'     => __('Country', 'woocommerce'),
            'options' => array(
                $geoData['country'] => $countries[$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'] => $countries[$geoData['country']]
            ),
            'class' => array(
                'form-row-wide',
                'address-field',
                'update_totals_on_change'
            )
        );
    return $fields;
    }
    add_filter( 'woocommerce_checkout_fields' , 'wpse_287199_woo_checkout_country' );
    /** Remove (optional) from country fields and make it non selectable*/
    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 of above code
    

    这将使国家/地区字段自动填充地理位置数据使其不可选择is there such a word? ;)

    非常欢迎任何改进建议。

    谢谢

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2014-10-29
      相关资源
      最近更新 更多