Установите налоговый класс для товаров корзины на основе выбранного значения поля оформления заказа WooCommerce.Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Установите налоговый класс для товаров корзины на основе выбранного значения поля оформления заказа WooCommerce.

Сообщение Anonymous »


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 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?


Источник: https://stackoverflow.com/questions/776 ... cted-value
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»