如何获取 WooCommerce 中的可用税率

2023-12-23

基于使用 WC_Tax::get_tax_classes() 获取所有 WooCommerce 税级 https://stackoverflow.com/questions/66196547/using-wc-taxget-tax-classes-to-get-all-woocommerce-tax-classes/66196816#66196816回答我之前的问题,我正在尝试从 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

如何获取包含可用税务详细信息(名称、税率等)的数组(在后端管理面板中)?


每个税级税率都是在后端通过位置设置定义的……因此它们取决于客户位置。产品和运输的税率可能有所不同。

对于每个税种,您需要按国家或运输区域设置税率,具体取决于允许的购买地点或区域(见下面的例子).

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

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

你会得到类似的东西:

有关的:使用 WC_Tax::get_tax_classes() 获取所有 WooCommerce 税级 https://stackoverflow.com/questions/66196547/using-wc-taxget-tax-classes-to-get-all-woocommerce-tax-classes/66196816#66196816


指定位置的用法

由于您似乎想在后端使用它,正如您在评论中所询问的那样,您需要指定一个位置才能获取该特定位置的税率。

例如,要获取法国国家设置的税率(“FR”国家代码)你将替换$location数组依据:

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

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

$location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 WooCommerce 中的可用税率 的相关文章

随机推荐

  • Angular,如何有条件地设置必填字段的样式?

    在按下提交按钮之前 我不想显示表单的验证 按下按钮时 如果有效 则应提交表单 如果未提交 则无效字段应以红色突出显示 由于遗留原因 我导入了一个 css 文件 它定义了input invalid具有红色背景 因此任何输入字段都标记为requ
  • 将 ../ 与 docker-compose 卷一起使用时会发生什么

    我在将文件从 docker 容器内写入主机时遇到问题 我相信这是一个特权问题并且不想设置privileged True 写出文件的解决方法是预先挂起 到我的一卷docker compose yml文件 例如 version 3 servic
  • 了解 VS2010 C# 并行分析结果

    我有一个包含许多独立计算的程序 因此我决定对其进行并行化 我使用 Parallel For Each 对于双核机器来说 结果还不错 大多数时候 CPU 利用率约为 80 90 然而 对于双 Xeon 机器 即 8 核 我只能获得大约 30
  • 延迟 SwiftUI 中的转换

    如何延迟过渡 我想单击一个按钮 然后视图应该延迟转换 我有以下代码 但它没有正确同步 struct ContentView View State var showOne true var body some View VStack if s
  • 调整 UILabel 的大小以适应插图

    我正在构建一个屏幕来扫描条形码 并且我需要在一些屏幕后面放置一个半透明屏幕UILabels以提高浅色背景下的可见度 现在屏幕如下所示 我正在设置背景颜色UILabel得到半透明的盒子 我还创建了一个自定义UILabel子类允许我在边缘之间设
  • 如何知道 S3 存储桶中存储了多少个对象?

    除非我遗漏了什么 否则我看过的 API 似乎都不会告诉你一个对象中有多少个对象
  • Monotouch打开文档-UIDocumentInterationController

    我想在我的 iPhone 应用程序上打开一个用 Monotouch 编写的文档 即在默认 PDF 查看器中启动 PDF 文件 我想我应该使用 UIDocumentInteractionController 任何人对此有任何想法 我已将以下内
  • 生成带有日期的随机时间序列数据

    我正在尝试生成带有日期的随机数据 整数 以便我可以在其上练习 pandas 数据分析命令并绘制时间序列图 temp depth acceleration 2019 01 1 0 218062 1 215978 1 674843 2019 0
  • Russell Libby 的 Pipes 组件

    有谁知道哪里可以下载 Russell Libby 的命名管道 成分 我能找到的所有链接都指向 http home roadrunner com rllibby source html http home roadrunner com rll
  • 在 htaccess 重写规则中附加秘密令牌作为查询字符串有多安全?

    我在尝试提供答案时做了一些测试mod rewrite 重定向但阻止直接访问 https stackoverflow com questions 45326795 mod rewrite redirect but prevent direct
  • 使用 CMake 和 conan 包管理器正确设置 Vulkan、glfw 和 spdlog

    我正在开发 Vulkan API 的渲染器 我在使用 CMake 和 conan 包管理器正确设置项目时遇到了很大的麻烦 让我们看一下我的 conanfile py 的依赖设置 from conans import ConanFile CM
  • 如何在Java中unpickle文件?

    我有一个由 Python 进程创建的 pickle 格式数据文件 实际上是 MCDungeon 缓存文件 我想从 Java 程序中读取此内容 为此 我使用了以下代码 public HashMap
  • 带图像的网格布局

    我正在尝试使用 HTML 和 CSS 为图像构建此网格布局 我想使用 div 而不是 table 但我不确定最好的方法是什么 我还需要在每张图片下面添加一个简短的描述 我使用浮动和padding bottom 底部填充用于模拟元素的高度 这
  • 在卸载之前将 AJAX 发送到服务器 [重复]

    这个问题在这里已经有答案了 所以据说从 Firefox gt 4 开始 https stackoverflow com questions 5398772 firefox 4 onbeforeunload custom message 将窗
  • 我需要帮助解码,这是 json 吗?

    这是json吗 我需要从中回显某些信息 但执行起来遇到困难 我尝试过json decode 但似乎不起作用 a 4 s 5 entry a 1 s 4 type s 10 individual s 5 group a 1 s 6 famil
  • 将二维数组分割成更小的二维数组

    有没有办法将 numpy 中的二维数组分割成更小的二维数组 Example 1 2 3 4 gt 1 2 3 4 5 6 7 8 5 6 7 8 所以我基本上想把一个 2x4 数组切成 2 个 2x2 数组 寻找用于图像的通用解决方案 有另
  • 根据另一列将值映射到特定列

    我有以下问题 A DataFrame含有col1带字符串A B or C 一秒col2与Integer 以及其他三栏col3 col4 and col5 这些列也被命名为A B and C Thus col1 col2 A col3 B c
  • 工作空间和工作目录是什么关系

    我一直在阅读有关 Perforce 的内容 但没有找到任何关于工作空间和工作目录之间关系的全面解释 例如文件如何出现在工作区的工作目录中 如何跟踪它们 工作区文件和工作目录文件之间可能存在哪些不一致等 我来自git背景 所以我正在寻找类似于
  • 子进程退出后内核复制 CoW 页面

    在Linux中 每当一个进程被fork时 父进程的内存映射都是cloned进入子进程 实际上 出于性能原因 页面被设置为写时复制 最初它们是共享的 如果两个进程之一在其中一个进程上写入 那么它们将被共享cloned MAP PRIVATE
  • 如何获取 WooCommerce 中的可用税率

    基于使用 WC Tax get tax classes 获取所有 WooCommerce 税级 https stackoverflow com questions 66196547 using wc taxget tax classes t