【问题标题】:How to get the available tax rates in WooCommerce如何在 WooCommerce 中获取可用的税率
【发布时间】:2021-02-14 16:22:55
【问题描述】:

根据Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes 对我上一个问题的回答,我正在尝试从 WooCommerce 中定义的税类中获取可用的税率。

我尝试了以下代码:

$tax_classes = wc_get_product_tax_class_options();

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

var_dump 导出为空,但看起来它与可用类交互。

/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

我如何获得一个包含可用税务详细信息的数组(在后端管理面板中):名称、tax_rate 等?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    每个税级税率都是在后端按位置设置定义的……因此它们取决于客户所在的位置。税率可以以不同的方式应用于产品和运输。

    对于每个税种,您需要根据允许的购买地点或区域,按国家或地区设置税率(参见下面的示例)

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

    $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>';
    }
    

    你会得到类似的东西:

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


    指定位置的用法

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

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

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

    如果没有指定国家,你将使用一个空值的数组,例如:

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

    【讨论】:

    • 感谢您的回答!但在这种情况下,我希望后端(管理面板)中的税收数据使用我的自定义发票插件中的税率。我没有国家或城市可以设置 $tax_rate_data。我想要一个 html 选择选项来处理 Woocommerce 中的 tax_rate。因此,Woocommerce 上的每个 tax_rate 都有自己的 html 选择选项。
    • @ThijmenvanDoorn 我添加了一个应该回答你的评论的补充......如果这个答案回答了你的问题,你可以请accept回答,如果你喜欢/想要你也可以请upvote答案也是,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-01-11
    • 2020-09-28
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多