如何在 WooCommerce 中获得可用的税率 [英] How to get the available tax rates in WooCommerce

查看:36
本文介绍了如何在 WooCommerce 中获得可用的税率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于

使用以下内容显示客户的可用税率(基于其位置):

$location = 数组('国家' =>WC()->客户->get_shipping_country() ?WC()->customer->get_shipping_country():WC()->customer->get_billing_country(),'状态' =>WC()->客户->get_shipping_state() ?WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),'城市' =>WC()->客户->get_shipping_city() ?WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),'邮政编码' =>WC()->客户->get_shipping_postcode() ?WC()->customer->get_shipping_postcode() : WC()->customer->get_billing_postcode(),);$输出=数组();//初始化(用于显示)//遍历税类foreach ( wc_get_product_tax_class_options() as $tax_class => $tax_class_label ) {//从客户位置和产品税类中获取税务数据$tax_rates = WC_Tax::find_rates( array_merge( $location, array( 'tax_class' => $tax_class ) ) );//最后我们得到税率(百分比数字)并显示出来:如果(!空($tax_rates)){$rate_id = array_keys($tax_rates);$rate_data = 重置($tax_rates);$rate_id = 重置($rate_id);//获取税率Id$rate = $rate_data['rate'];//获取税率$rate_label = $rate_data['label'];//获取税号$is_compound = $rate_data['compound'];//税率是否复合$for_shipping = $rate_data['shipping'];//税率是否用于运费//设置为显示$output[] = '<li class="tax-rate '.$tax_class.'">'.$tax_class_label.': <strong>'.$rate.'</strong><small>(label: '.$rate_label.' | id: ' . $rate_id . ')</small></li>';}}//展示如果(!空($输出)){echo '

'.__(可用税率").'</h4><ul>'.内爆('', $output) .'</ul></div>';}

你会得到类似的东西:

相关:使用WC_Tax::get_tax_classes() 获取所有 WooCommerce 税级


指定位置的用法

由于您似乎想在评论中要求的那样在后端使用它,因此您需要指定一个位置才能获得该特定位置的税率.

例如,要获取为法国国家/地区设置的税率(FR"国家/地区代码),您需要将 $location 数组替换为:

$location = array( 'country' => 'FR', 'state' => '', 'city' => '', 'postcode' => '' );

如果没有指定国家/地区,您将使用空值数组,例如:

$location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );

Based on Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes answer to my previous question, I am trying to get the available tax rates from the defined tax-classes in WooCommerce.

I tried the following code:

$tax_classes = wc_get_product_tax_class_options();

foreach ( $tax_classes as $tax_class ) {
    $taxes = WC_Tax::get_rates( $tax_class );
    var_dump($taxes);
}

The var_dump exports are empty, but it looks like it interacts with the available classes.

/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty
/home/public_html/...etc
array (size=0)
  empty

How do i get a array (in the backend admin-panel) with the available tax details: name, tax_rate etc?

解决方案

Each tax class rates are defined in backend by location settings… so they depend on customer location. The tax rates can be applied differently to products and shipping.

For each tax class, you need to set tax rates by country or by shipping zone, depending on allowed purchase locations or zones (see the example below).

Use the following to display the available tax rates for the customer (based on its location):

$location = array(
    'country'   => WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country(),
    'state'     => WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),
    'city'      => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
    'postcode'  => WC()->customer->get_shipping_postcode() ? WC()->customer->get_shipping_postcode() : WC()->customer->get_billing_postcode(),
);

$output = array(); // Initialiizing (for display)

// Loop through tax classes
foreach ( wc_get_product_tax_class_options() as $tax_class => $tax_class_label ) {

    // Get the tax data from customer location and product tax class
    $tax_rates = WC_Tax::find_rates( array_merge(  $location, array( 'tax_class' => $tax_class ) ) );

    // Finally we get the tax rate (percentage number) and display it:
    if( ! empty($tax_rates) ) {
        $rate_id      = array_keys($tax_rates);
        $rate_data    = reset($tax_rates);

        $rate_id      = reset($rate_id);        // Get the tax rate Id
        $rate         = $rate_data['rate'];     // Get the tax rate
        $rate_label   = $rate_data['label'];    // Get the tax label
        $is_compound  = $rate_data['compound']; // Is tax rate compound
        $for_shipping = $rate_data['shipping']; // Is tax rate used for shipping

        // set for display
        $output[] = '<li class="tax-rate '.$tax_class.'">'.$tax_class_label.': <strong>'.$rate.'</strong> <small>(label: '.$rate_label.' | id: ' . $rate_id . ')</small></li>';
    }
}

// Display
if ( ! empty($output) ) {
    echo '<div><h4>' . __("Available Tax rates") . '</h4><ul>' . implode('', $output) . '</ul></div>';
}

You will get something like:

Related: Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes


Usage specifying a location

As it seems that you want to use it in backend as are asking in your comment, you need to specify a location to be able to get tax rates for that specific location.

So for example to get the tax rates set for France country ("FR" country code) you will replace the $location array by:

$location = array( 'country' => 'FR', 'state' => '', 'city' => '', 'postcode' => '' );

If there is no country specified, you will use an array with empty values like:

$location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );

这篇关于如何在 WooCommerce 中获得可用的税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆