I’m currently building a custom form. In this form I want to display the country select which is already present at the checkout.
This is my tax settings list:
This is my code:
<select class="country-select">
<?php
foreach ( WC_Tax::get_rates() as $rate ) {
echo '<option value="' . $rate->tax_rate . '">' . $rate->country . '</option>';
}
?>
</select>
I’m expecting following options:
<option value="19">Deutschland</option>
<option value="20">Österreich</option>
The problem is that I don’t get any results back from the get_rates()
function. I’ve found no function which can return me the correct rates. Do you have any idea how I can get this done?
2
Answers
If you’ve got tax rates configured, it’s possible that they don’t cover whatever user you’re logged in as.
Try feeding the
$tax_class
, and$customer
arguments to yourWC_Tax::get_rates
method! Specifically a customer and tax class that the Deutschland and/or Österreich rates from your example apply to!Review the actual function definition here:
TL;DR
Explanation:
As far as I know, you can only get a list of all tax rates given a specific
tax_class
, which is used to filterwoocommerce_tax_rates
table bytax_rate_class
column (well, we can always query the database directly, but I’m assuming you want to solve the problem using functions provided by WooCommerce). Also, “Standard rates” have notax_class
, ie.tax_rate_class
column is an empty string.So if you want to get all tax rates per class, you need to use
WC_Tax::get_rates_for_tax_class( $tax_class )
. But if you really want all tax rates regardless of class, you need to get all tax classes first, then get all tax rates per class.