【问题标题】:Get tax rate separately for every cart and order items in Woocommerce分别获取 Woocommerce 中每个购物车和订单商品的税率
【发布时间】:2018-12-23 17:28:33
【问题描述】:

我想修改购物车显示(以及后来的发票),以便有另一列显示每种产品的税金和税率。我没有找到作为数字的税率的函数或吸气剂,只有名称,$_product->get_tax_class()。我一直在寻找像 $_product->get_tax_rate() 这样的函数,但没有找到。所以我在 woocommerce/templates/cart/cart.php 中写了一个糟糕的解决方法。

在添加的简单部分之后

<th class="product-tax"><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th>

在第 35 行,我从第 121 行添加:

$tax_name = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
if ($tax_name == "reduced-tax-rate") $tax_rate= 7; else $tax_rate= 19;                      
$with_tax = $_product->get_price( $_product ); 
$without_tax = $with_tax/((100+$tax_rate)/100);
$tax = $with_tax-$without_tax;
$tax = $tax*$cart_item['quantity'];
$tax = number_format($tax, 2, '.', '')." €";
echo "  ".$tax." (".$tax_rate."%)";

这目前有效,但仅在德国有效,而且它当然不会存活很长时间。那么,有什么更好的方法来做到这一点?

谢谢!

更新

刚刚找到了一半的解决方案:

$tax_name = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
if ($tax_name == "reduced-tax-rate") $tax_rate= 7; else $tax_rate= 19;                      
echo "  ".$cart_item['line_subtotal_tax']." (".$tax_rate."%)";

$cart_item['line_subtotal_tax'] 包含我试图通过计算获得的值。现在只是缺少名称...“19%”或“7%”...

【问题讨论】:

    标签: php wordpress woocommerce cart tax-rate


    【解决方案1】:

    2020 年 10 月更新 (删除了一些错误 - 在 WooCommerce 4.5.x 版本上测试)

    我想woocommerce_cart_item_tax 是一个自定义钩子,因为我没有找到它……

    税费取决于您的设置,即一个或多个税级以及每个税级:

    • 所有国家/地区或/和按国家/地区
    • 一个国家的所有州或/和各州
    • 一个国家/地区的所有邮政编码或/和邮政编码
    • 一个或多个税率。
    • (和其他设置)

    现在要以正确的方式处理税款,您将使用the WC_Tax object Class and all related methods。我们将在此处仅使用基于国家/地区的税率:

     // Getting an instance of the WC_Tax object
     $wc_tax = new WC_Tax();
    
    // Get User billing country
    $billing_country = WC()->customer->get_billing_country();
    
    // Get the item tax class (your code)
    $tax_class = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
    
    // Get the related Data for Germany and "default" tax class
    $tax_data = $wc_tax->find_rates( array('country' => $billing_country, 'tax_class' => $tax_class ) );
    
    // Get the rate (percentage number)
    if( ! empty($tax_data) ) {
        $tax_rate = reset($tax_data)['rate'];
    
        // Display it
        printf( '<span class="tax-rate">' . __("The tax rate is %s", "woocommerce") . '</span>',  $tax_rate . '%' );
    }
    

    经过测试并且有效。


    对于订单 (pdf 发票) 应该非常相似,您需要替换此行:

    // Get the item tax class (your code)
    $tax_class = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
    

    通过类似的方式(其中$itemWC_Order_Item_Product 对象的实例):

    // Get the WC_Product Object instance
    $_product = $item->get_product();
    
    // Get the item tax class
    $tax_class = $_product->get_tax_class();
    

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2017-03-26
      • 2021-03-14
      • 2021-05-28
      • 2021-05-16
      相关资源
      最近更新 更多