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;
}
Код: Выделить всё
billing_legal_person
I also managed to force the recalculation of the taxes when changing the content of my
Код: Выделить всё
billing_legal_person
Код: Выделить всё
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_set_taxrate_for_billing_legal_person()
Код: Выделить всё
billing_legal_person
If I check
Код: Выделить всё
WC()->checkout->get_checkout_fields['billing_legal_person']
If I check
Код: Выделить всё
WC()->checkout->checkout->get_value('billing_first_name')
Код: Выделить всё
WC()->checkout->checkout->get_value('billing_legal_person')
Код: Выделить всё
NULL
Код: Выделить всё
billing_first_name
Код: Выделить всё
billing_legal_person
All approaches based on checking
Код: Выделить всё
$_POST['billing_legal_person']
Any help on how I could access the content of the
Код: Выделить всё
billing_legal_person
Код: Выделить всё
my_set_taxrate_for_billing_legal_person()
Источник: https://stackoverflow.com/questions/776 ... cted-value