结帐-根据地理位置限制帐单和送货国家/地区

时间:2019-02-14 05:50:16

标签: woocommerce

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
        }

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

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

What I expect to be seen

非常感谢

1 个答案:

答案 0 :(得分:0)

好吧,我无法完全实现我想要达到的目标,但得到了一些收获

我的最终代码:

/** 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? ;)

final result

任何改进建议都非常受欢迎。

谢谢

相关问题