Php Woocommerce条件结帐字段和基于国家/地区和购物车总额的欧盟增值税
在woocommerce中,我启用了woocommerce EU VAT插件,并创建了一个必需的自定义签出选择字段Customer type,该字段有两个选项: 个人 商业 现在,我正在尝试显示并启用欧盟增值税字段: 订单最多500张 “客户类型”仅适用于“业务”, 国家:仅丹麦和芬兰。 这是我的密码:Php Woocommerce条件结帐字段和基于国家/地区和购物车总额的欧盟增值税,php,jquery,wordpress,woocommerce,checkout,Php,Jquery,Wordpress,Woocommerce,Checkout,在woocommerce中,我启用了woocommerce EU VAT插件,并创建了一个必需的自定义签出选择字段Customer type,该字段有两个选项: 个人 商业 现在,我正在尝试显示并启用欧盟增值税字段: 订单最多500张 “客户类型”仅适用于“业务”, 国家:仅丹麦和芬兰。 这是我的密码: add_filter('woocommerce_checkout_fields', 'add_eu_vat_to_checkout'); function add_eu_vat_to_chec
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插件尝试以下测试:
// 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添加EU VAT字段,例如 以下是演示URL:
还有一些WooCommerce函数,您可以在这些函数中添加一个字段。@LoicTheAztec我已经更新了代码。你介意看一下吗?你走错了路。以这种方式混合使用PHP和jquery是个坏主意。你打破了关注点分离的原则。尽量避免这种做法,并找到另一种解决办法。@Jacobian我不是一个编码员,我能读一些,但不能完全理解它们是如何工作的。这就是我在这里寻求帮助的全部原因。您建议我怎么做?如果是由Woocomece开发者团队完成的,并不意味着它是正确的。整个团队可能会遵循一些混乱的设计。@Jacobian谢谢你提供的信息,所以问题是:你有什么建议?我真的很想让它运行起来: