【问题标题】:How can I get all available tax rates in WooCommerce?如何在 WooCommerce 中获得所有可用的税率?
【发布时间】:2019-09-09 23:10:24
【问题描述】:

我目前正在构建一个自定义表单。在此表单中,我想显示结帐时已经存在的国家/地区选择。

这是我的税务设置列表:

这是我的代码:

<select class="country-select">
    <?php
    foreach ( WC_Tax::get_rates() as $rate ) {
        echo '<option value="' . $rate->tax_rate . '">' . $rate->country . '</option>';
    }
    ?>
</select>

我期待以下选项:

<option value="19">Deutschland</option>
<option value="20">Österreich</option>

问题是我没有从get_rates() 函数返回任何结果。我没有找到可以返回正确费率的函数。你知道我怎样才能完成这项工作吗?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    TL;DR

    $all_tax_rates = [];
    $tax_classes = WC_Tax::get_tax_classes(); // Retrieve all tax classes.
    if ( !in_array( '', $tax_classes ) ) { // Make sure "Standard rate" (empty class name) is present.
        array_unshift( $tax_classes, '' );
    }
    foreach ( $tax_classes as $tax_class ) { // For each tax class, get all rates.
        $taxes = WC_Tax::get_rates_for_tax_class( $tax_class );
        $all_tax_rates = array_merge( $all_tax_rates, $taxes );
    }
    

    说明:

    据我所知,你只能得到一个给定特定tax_class的所有税率的列表,它用于通过tax_rate_class列过滤woocommerce_tax_rates表(好吧,我们总是可以直接查询数据库,但我假设您想使用 WooCommerce 提供的功能解决问题)。此外,“标准费率”没有tax_class,即。 tax_rate_class 列是一个空字符串。

    因此,如果您想获得每个班级的所有税率,您需要使用WC_Tax::get_rates_for_tax_class( $tax_class )。但是,如果您真的想要所有税率而不考虑类别,则需要先获取所有税类,然后获取每个类别的所有税率。

    【讨论】:

      【解决方案2】:

      如果您配置了税率,则它们可能不涵盖您登录的任何用户。

      尝试将$tax_class$customer 参数提供给您的WC_Tax::get_rates 方法!特别是您示例中的德国和/或 Österreich 税率适用的客户和税类!

      查看实际函数定义here

      <?php
      
      // class-wc-tax.php
      
      class WC_Tax {
      // ...
          /**
           * Get's an array of matching rates for a tax class.
           *
           * @param string $tax_class Tax class to get rates for.
           * @param object $customer Override the customer object to get their location.
           * @return  array
           */
          public static function get_rates( $tax_class = '', $customer = null ) {
              $tax_class         = sanitize_title( $tax_class );
              $location          = self::get_tax_location( $tax_class, $customer );
              $matched_tax_rates = array();
      
              if ( count( $location ) === 4 ) {
                  list( $country, $state, $postcode, $city ) = $location;
      
                  $matched_tax_rates = self::find_rates(
                      array(
                          'country'   => $country,
                          'state'     => $state,
                          'postcode'  => $postcode,
                          'city'      => $city,
                          'tax_class' => $tax_class,
                      )
                  );
              }
      
              return apply_filters( 'woocommerce_matched_rates', $matched_tax_rates, $tax_class );
          }
      }
      

      【讨论】:

      • 不,只是用不同的方式
      • 嘿,@Mr.Jo - 你最终解决了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2020-09-28
      • 2021-04-25
      相关资源
      最近更新 更多