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

时间:2019-04-29 09:11:59

标签: php wordpress woocommerce

信息:

WooCommerce版本:3.6.2

WordPress版本:5.1.1

YITH WooCommerce要求YITH报价溢价– 2.1.7


目标:

如果客户使用报价进行结帐,则保留使用报价设置的开票和运输国家/地区,否则使用地理位置来限制国家/地区

如果有帮助,发送给客户的报价URL如下所示: mysite / request-quote /?request_quote = 330&status = accepted&raq_nonce = ad5c139f3d7a59993a4ff679d84de3a3&lang


我的密码尝试

// 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

更新03 / May / 2019

此代码无效。

欢迎对此提供任何帮助。

2 个答案:

答案 0 :(得分:0)

我终于考虑了这一点,并提出了不同的解决方案。

第1部分-设置 在Woocommerce->设置->常规->默认客户位置=地理位置 Woocommerce setting

第2部分-代码 代码进入您的活动子主题(或活动主题)的function.php文件中。

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

这暂时对我有效。如果有人可以提出更好的解决方案,我将不胜感激。

答案 1 :(得分:-1)

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