In WooCommerce, I need to add a "billing_legal_person" custom field to my Checkout page that is selectable between "Person" and "Company" to reflect whether the customer should be taxed as a company or as a person. Then, the applicable VAT percentage should be different according to the content of that billing_legal_person selection.
I have successfully created 2 tax classes: the default (standard) one for persons and a new "companies" class for companies.
I can also successfully apply programmatically one tax class or the other with this code:
add_filter( 'woocommerce_product_get_tax_class', 'my_set_taxrate_for_billing_legal_person', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'my_set_taxrate_for_billing_legal_person', 10, 2 );
function my_set_taxrate_for_billing_legal_person( $tax_class, $product ) {
// Commenting the line below, standard tax class is applied
$tax_class = 'companies';
return $tax_class;
}
I used Checkout Field Editor plugin to easily create the billing_legal_person
select field (values ‘person’ or ‘company´).
I also managed to force the recalculation of the taxes when changing the content of my billing_legal_person
custom field:
add_filter( 'woocommerce_checkout_fields', 'my_checkout_fields_trigger_refresh', 9999 );
function my_checkout_fields_trigger_refresh( $fields ) {
$fields['billing']['billing_legal_person']['class'][] = 'update_totals_on_change';
return $fields;
}
My problem is that inside my_set_taxrate_for_billing_legal_person()
, I’m unable to access the content of the billing_legal_person
custom field selected by the user.
If I check WC()->checkout->get_checkout_fields['billing_legal_person']
, I always get an empty value.
If I check WC()->checkout->checkout->get_value('billing_first_name')
, I get the intended value (customer first name). However, if I then check WC()->checkout->checkout->get_value('billing_legal_person')
, I get a NULL
value. I suppose I can access billing_first_name
because it’s a standard field but I can’t access billing_legal_person
because it is a custom field, but I could not find any clue on how to access custom fields.
All approaches based on checking $_POST['billing_legal_person']
fail because I need to show the amount of taxes to be paid BEFORE the user submits the check-out form.
Any help on how I could access the content of the billing_legal_person
custom field from my_set_taxrate_for_billing_legal_person()
so I can check it and return one tax class or another?
2
Answers
First, don’t use Checkout field editor plugin to add ‘billing_legal_person’ custom checkout field, instead we will use a hand coded function.
Also, remove your
my_set_taxrate_for_billing_legal_person()
function.Here JavaScript, Ajax and WC_Session are required to achieve your goal.
The following code below will:
The Code (commented):
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Just found your code (which is exactly what I needed, thank you very much for it) but I’m having an issue I cannot save: The code works perfectly and applies the taxes correctly depending on the legal user chosen, but when I repeat the purchase logged in with the same account and try to purchase the same or different product (actually I only have one with two variations), the tax rate applied is the same of the first purchase, even if I change the billing legal person (the taxes applied on the checkout are the same that the legal person I chose on the first purchase and the total amount doesn’t update). Because of the business type, there’s a chance the customers may want to purchase an item for their company, with the corresponding tax deductions, and, later, another item as a regular customers and I’m really struggling trying to fix it. Could you help me? Any thoughts?
Thank you very much in advance <3