Woocommerce条件结帐字段和基于国家和购物车总额的欧盟增值税 [英] Woocommerce conditional checkout fields and Eu VAT based on country and cart total
本文介绍了Woocommerce条件结帐字段和基于国家和购物车总额的欧盟增值税的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在woocommerce中,我启用了Woocommerce EU VAT插件,并创建了具有2个选择的必需的自定义结帐选择字段客户类型":
In woocommerce I have enabled Woocommerce EU VAT plugin and created a required custom checkout select field "Customer type" with 2 choices:
- 个人
- 业务
现在,我尝试显示并启用以下项的欧盟增值税"字段:
Now I am trying to show and enable EU VAT field for:
- 订单金额最多只能为
500
-
'customer_type'
仅用于'business'
, - 国家/地区:仅丹麦和芬兰.
- Orders amount up to
500
only 'customer_type'
for'business'
only,- Countries: Denmark and Finland only.
这是我的代码:
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' );
}
}
}
感谢您的帮助.
推荐答案
最后不需要Ajax.尝试以下方法(已通过WooCommerce EU VAT插件测试):
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'] ) );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中..经过测试和工作.
这篇关于Woocommerce条件结帐字段和基于国家和购物车总额的欧盟增值税的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文