问题
In woocommerce I have enabled Woocommerce EU VAT plugin and created a required custom checkout select field "Customer type" with 2 choices:
- Individual
- Business
Now I am trying to show and enable EU VAT field for:
- Orders amount up to
500
only 'customer_type'
for'business'
only,- Countries: Denmark and Finland only.
Here is my code:
add_filter('woocommerce_checkout_fields', 'add_eu_vat_to_checkout');
function add_eu_vat_to_checkout() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) || ! is_checkout() )
return;
$customer_type_value = WC()->session->get( 'customer_type' );
$subtotal = $wc_cart->subtotal;
$minimum_order_subotal = 500;
if ($customer_type_value == 'Business' && $minimum_order_subtotal > 500)
{
add_filter( 'woocommerce_eu_vat_number_country_codes', 'woo_custom_eu_vat_number_country_codes' );
function woo_custom_eu_vat_number_country_codes( $vat_countries ) {
// only show field for users in Denmark and Finland
return array( 'DK', 'FI' );
}
}
}
Any help is appreciated.
回答1:
Finally no need of Ajax. Try the following (tested with WooCommerce EU VAT plugin):
// Add "Customer type" checkout field (For testing) - To be removed if you got it
add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields', 20, 1 );
function add_custom_checkout_fields( $fields ) {
// Get the "Customer type" if user is logged in
if(is_user_logged_in() )
$value = get_user_meta( get_current_user_id(), 'customer_type', true );
$fields['billing']['customer_type'] = array(
'type' => 'select',
'label' => __('Customer type', 'woocommerce'),
'options' => array(
'' => __('Please, select your type'),
'individual' => __('Individual'),
'business' => __('Business'),
),
'required' => true, // required
'class' => array('form-row-wide'),
'clear' => true,
);
// Set the "Customer type" if is not empty (from user meta data)
if( ! empty($value) )
$fields['billing']['customer_type']['default'] = $value;
return $fields;
}
// Enabling Eu Vat for ('DK' and 'FI') when cart amount is up to 500
add_filter( 'woocommerce_eu_vat_number_country_codes', 'woo_custom_eu_vat_number_country_codes', 20, 1 );
function woo_custom_eu_vat_number_country_codes( $vat_countries ) {
// HERE below your settings
$countries = array( 'DK', 'FI' );
$min_amount = 500;
$cart_items_amount = WC()->cart->cart_contents_total;
// Avoiding errors on admin and on other pages
if( is_admin() || WC()->cart->is_empty() )
return $countries;
// Show EU VAT field for cart amount up to 500 & users in Denmark and Finland
return $cart_items_amount >= $min_amount ? $countries : array();
}
add_action( 'wp_footer', 'custom_checkout_jquery_script', 30, 1 );
function custom_checkout_jquery_script( $checkout ) {
if( !is_checkout() ) return; // Only checkout
?>
<script type="text/javascript">
(function($){
var a = 'select[name=customer_type]',
b = 'business',
i = 'individual',
bc = '#billing_company_field',
lbc = 'label[for=billing_company]',
lr = lbc + ' > .required',
r = '<abbr class="required" title="required">*</abbr>',
vat = '#vat_number_field';
// On start (once DOM is loaded)
$('label[for=vat_number]').append(r); // Mark Eu Vat required
// Hide EU VAT if not business and other needed things
if( b != $(a).val() && $(vat).length ) {
$(vat).fadeOut('fast'); // Hide EU Vat field
// If is an individual we hide company field
if( i == $(a).val())
$(bc).fadeOut(); // Hide company
// Mark company field as required
} else if( b == $(a).val() && $(vat).length ) {
$(lbc).append(r); // Company required
}
// On "Customer Type" live event
$('form.checkout').on('change', a, function(e){
e.preventDefault();
// Show EU VAT and company For "business" with other needed things
if( b == $(a).val() ){
if( $(vat).length )
$(vat).fadeIn(); // Show EU Vat field
$(lbc).append(r); // Company required
$(bc).fadeIn(); // Show Company
} else if( i == $(a).val()) { // For "individual"
if( $(vat).length )
$(vat).fadeOut(); // Hide EU Vat field
$(lr).remove(); // Remove Company required
$(bc).fadeOut(); // Hide Company
} else { // Nothing selected
if( $(vat).length )
$(vat).fadeOut(); // Hide EU Vat field
$(lr).remove(); // Remove Company required
$(bc).fadeIn(); // Show Company
}
});
})(jQuery);
</script>
<?php
}
// Update Order and User meta data for "Customer Type"
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
// Set customer type in the order and in user_data
if( ! empty($_POST['customer_type']) ){
// Update Order meta data for 'customer_type'
$order->update_meta_data( '_customer_type', sanitize_key( $_POST['customer_type'] ) );
// Update User meta data for 'customer_type'
if( $order->get_user_id() > 0 )
update_user_meta( $order->get_user_id(), 'customer_type', sanitize_key( $_POST['customer_type'] ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
回答2:
There are several plugins available for adding EU VAT field for WooCommerce such as
https://wppluginninja.us/demo/product/woocommerce-vat-number-addon/
Here is the Demo URL: https://wppluginninja.us/demo/product/woocommerce-vat-number-addon/
As well as there are WooCommerce functions you can add a field to those functions.
来源:https://stackoverflow.com/questions/49418569/woocommerce-conditional-checkout-fields-and-eu-vat-based-on-country-and-cart-tot