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 = ' <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.